11

I am considering moving from Hibernate to jOOQ but I am not sure if I can do without caching. Hibernate has a first- and second-level cache. I know that jOOQ does have support for reusing prepared statements.

Will I have to take care of caching on my own if I use jOOQ?

Community
  • 1
  • 1
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378

2 Answers2

10

Query caching / result caching:

I'm mentioning this, because this kind of cache is also possible with Hibernate, and it might make sense under certain circumstances.

In Hibernate, the query cache works closely with the second-level cache. In jOOQ, you can implement a query cache that intercepts all queries using the jOOQ VisitListener API. There are some blog articles about this topic:

There will be better support for this type of cache in the future (not in jOOQ 3.7, yet), as this kind of cache belongs in a SQL API. Note that your JDBC driver might also support this kind of cache already - e.g. ojdbc does.

First-level caching:

The idea behind Hibernate's first level cache doesn't make sense with a more SQL-oriented API like jOOQ. SQL is a highly complex language that works between the actually persisted entities, and your client representation of the same entities. This means that once you use SQL, you will probably create ad-hoc tuples that have nothing to do with the original entity representation of your data.

In other words: First-level caching is a thing that is only possible if you limit the functionality and scope of your query language, and if you take control over ALL your database interactions, the way Hibernate does it. jOOQ expressly doesn't do that.

Second-level caching:

The second-level cache in Hibernate is a cache that is mostly useful with master data and similar types of data where fetching the data from the database hardly ever makes sense, as the data doesn't change.

There is no reason at all, why an ORM should implement this kind of cache, short of convenience in simple cases. But in many cases, you're better off annotating a service method with @Cacheable, e.g. as documented here on this Spring page. This would be a cache on a higher layer than on the ORM / query layer, where you will be able to correlate caching much more tightly with your business logic.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • Thank you for that excellent answer! It helps my with my decision making a lot! – Stefan Falk Sep 24 '15 at 14:08
  • @StefanFalk: You're welcome. That is going to be a decision in favour of jOOQ, I hope :) – Lukas Eder Sep 27 '15 at 07:56
  • Personally I'm tending to jOOQ but it's still a hard decision for me because I don't know how much work Hibernate actually took off my shoulders when I was using it. I'm "*refactoring* a project that I've been working in for half a year now but I'm not alone anymore. I made myself responsible for the backend and I don't want to screw things up. This week I'm going to use jOOQ in order to create the Java files and I will implement a few Repositories and Services to test it and get a feeling of it :) – Stefan Falk Sep 28 '15 at 09:38
  • But is there a chance that you know some projects on github/bucket/etc. which use jOOQ? I would be interested in reading code of some existing projects if there are one. :) – Stefan Falk Sep 28 '15 at 09:40
  • 1
    @StefanFalk: jOOQ is not a drop-in replacement for Hibernate. The two APIs are solving different problems. [This blog post might be interesting for you](http://blog.jooq.org/2015/03/24/jooq-vs-hibernate-when-to-choose-which/). Also, you don't have to make a decision. You can use both. [Here's how to write type safe native SQL queries to query entities in Hibernate, with jOOQ](http://blog.jooq.org/2015/05/26/type-safe-queries-for-jpas-native-query-api/). Some OSS example projects include: https://github.com/rancher/cattle, https://github.com/torodb/torodb. – Lukas Eder Sep 28 '15 at 10:37
  • @StefanFalk: More sophisticated examples are closed source, I'm afraid. You could try asking for examples on http://www.reddit.com/r/java, though. – Lukas Eder Sep 28 '15 at 10:38
2

Yes, you will. jOOQ is just a type-safe way of doing SQL and does not do any caching.

artbristol
  • 32,010
  • 5
  • 70
  • 103