Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JPA/Hibernate be combined with other persistence framework like jOOQ

We have domain that where 90% of the classes are very straightforward and can be easily mapped 1:1 in the DB. I am very happy how Hibernate combined with spring-data-jpa removes tons of chores for these classes.

The rest of the domain however is challenging and for number of reasons I don't want to directly map it to DB tables.

I did experiment to introduce intermediate beans that are managed by Hibernate and map from these beans to my domain and this worked well when all relations are from the challenging to the easy part. This approach fails when I have "easy" class managed by Hibernate that references "challenging" class that is mapped in custom Java code and not directly hibernate managed.

This is when I realized I cannot find no way to customize Hibernate and plug in some sort of ObjectFactory that would allow me to do such transformations on the fly.

--edit--

What is my question: What is the easiest way to have DDD-style domain layer that has zero DB concerns in the entities, while using JPA? In DDD all DB concerns are handled by the Repository which typically collaborates with DAOs.

Zero DB concerns in the entities means there are no JPA annotations or mapping configurations in the Domain classes. One way to do it is to have JPA (or other persistence technology) manage TOs that are mapped to the Domain Entities. If I take this route however I have to have all of my entities, even the simplest one (think Address) go via the mapping layer.

I would like to use something "lazy" like JPA for the trivial entities and be able to mix them with the other entities that are "manually" managed. Currently I do not know clever solution that allows me to have link from JPA managed entity to non-JPA managed entity. I can always retrieve the JPA entity and then retrieve the non-JPA entity with second call but would like to avoid this if possible.

like image 331
gdanov Avatar asked Nov 26 '12 22:11

gdanov


People also ask

Can we use JPA and Hibernate together?

You cant actually use both of them in the same application. For backwards compatibility. We are expanding our application and want to start using Spring Data JPA, but still keep the old hibernate implementation. Its better you develop your new application as a separate microservice and use spring data jpa ..

Does jOOQ use Hibernate?

Compared to other popular libraries like Hibernate, jOOQ takes a database-first or SQL-centric approach. With Hibernate, you usually start writing your Java classes first and then let Hibernate or a tool like Liquibase or Flyway generate corresponding database tables. With jOOQ, you start with your database.

Can we use both JDBC and JPA together?

JPA-based applications still use JDBC under the hood. Therefore, when we utilize JPA, our code is actually using the JDBC APIs for all database interactions. In other words, JPA serves as a layer of abstraction that hides the low-level JDBC calls from the developer, making database programming considerably easier.

What is the difference between jOOQ and JPA?

JOOQ gives you full control because you can read and write the actual query in your code but with type safety. JPA embraces the OO model and this simply does not match the way a database works in all cases, which could result in unexpected queries such as N+1 because you put the wrong annotation on a field.


1 Answers

You can combine JPA and jOOQ. JPA/Hibernate are great for writing data:

  • all entity columns are included in the INSERT/UPDATE, so you don't have to change the save routines when adding new columns
  • extensive support for both implicit/explicit optimistic/pessimistic locking
  • support for optimized identifier generators (pooled-lo optimizer)
  • transactional write-behind cache to reduce lock contention (even in MVCC DBs)
  • JDBC fetching is just one configuration away

SQL and jOOQ are great for reading data and especially when you want to go beyond the SQL-92 JPA Dialect abstraction. With native queries (and type-safe jOOQ), you can take advantage of every feature your database has to offer:

  • Window functions
  • PIVOT
  • Derived tables
  • Common Table Expressions and Recursive queries
  • MERGE
  • Bulk INSERTS/UPDATES

In the end, you will most likely need both JPA and native queries, so jOOQ is a great choice for getting the most of your current database while giving you ca compile-time safety guarantees.

like image 83
Vlad Mihalcea Avatar answered Oct 18 '22 23:10

Vlad Mihalcea