Spring Data JDBC / Spring Data JPA vs Hibernate

As @Naros said, the question as it is currently in the title doesn’t really work. It seems we should really look at 4 options and mostly list the pros of each approach, the cons are the absence of the pros of the other:

JDBC without Spring Data

You get 100% fine-grained control over what is happening. Nothing gets generated or injected by a framework. This might sound like a con, but if you have tried to tweak mappings and configurations to get some JPA implementation to do what you could trivially write down in java and SQL, you’ll understand that this can be a big pro.

You don’t have to learn JPA, nor Spring Data. I personally think Spring Data is easy, but I’m biased (see my Profile). But JPA is most certainly challenging, once you leave the area of trivial entities and setup.

  • no requirements how you model your domain model (JPA requires default constructors for example)

You probably want to use some library in order to cut down on boilerplate code. Take a look at:

  • JOOQ

  • MyBatis

  • Spring JdbcTemplate (usable without the rest of Spring)

  • QueryDsl

JDBC with Spring Data

You get the benefits of Spring Data, combined with those of JDBC (see above):

  • Repositories with CRUD methods out of the box.

  • Support for Aggregates. See https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates

  • Nice integration in the Spring infrastructure, for transaction handling, dependency injection, error translation, paging …

  • It is still a really simple programming model. SQL statements happen exactly when one would expect them to happen and if you want to you can fall back to simple JDBC with or without the support of other frameworks, without breaking any abstraction.

  • Nice and easy ways to extend your repositories with query methods (you just define your interface to have a findByLastName method and Spring generates it for you on the fly) or @Query annotations or custom methods.

  • Support for paging

Hibernate (or some other JPA implementation) without Spring Data

JPA does a lot of things over JDBC

  • Caching (1st, 2nd level, and query cache)

  • Automated creation of instances from queries

  • Navigation between entities

  • Lazy loading

  • Dirty checking / tracking of changes to entities

With all this stuff happening it can be difficult understanding what is happening and why. Of course IFF you structure your application properly, you can just fall back on JDBC if JPA doesn’t offer what you want. But I have seen it multiple times that people failed to maintain the structure required for that to work. Obviously, this is especially difficult if you don’t understand properly how JPA works.

Hibernate (or some other JPA implementation) with Spring Data

I listed the benefits of Spring Data above, just perform a mental copy&paste.

Of course, this makes the complete stack even more complex. From the many questions tagged with AND it seems many developers have problems identifying which tool does what. But also from looking at these questions most describe problems with Hibernate/JPA and not Spring Data.

To wrap it up:

  • If you want/need fine-grained control use JDBC.

  • If you’re going to use JPA, make sure you understand it early on.

  • If for the persistence technology you are choosing Spring Data offers a module, I would use it. It will make life easier. But again I’m biased.

Leave a Comment