Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JDBC / Spring Data JPA vs Hibernate

What are the typical real life scenarios where one would choose Spring Data JDBC / Spring Data JPA vs Hibernate? I would like to understand the scenarios where either of these implementations are best suited.

like image 656
Punter Vicky Avatar asked Feb 26 '17 15:02

Punter Vicky


People also ask

Which is better spring data JPA or Hibernate?

Conclusion. Hibernate is a JPA provider and ORM that maps Java objects to relational database tables. Spring Data JPA is an abstraction that makes working with the JPA provider less verbose. Using Spring Data JPA you can eliminate a lot of the boilerplate code involved in managing a JPA provider like Hibernate.

What is the difference between spring JDBC and spring data JPA?

Spring JDBC only helps with the connection to the database and with executing queries on this database. Spring Data JPA wants to help you manage your JPA based repositories. It wants to remove boilerplate code, make implementation speed higher and provide help for the performance of your application.

Which is better spring JDBC or spring JPA?

Also, JPA is thought to be better suited for more sophisticated applications by many developers. But, JDBC is considered the preferable alternative if an application will use a simple database and we don't plan to migrate it to a different database vendor.


1 Answers

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 spring-data AND hibernate 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.

like image 184
Jens Schauder Avatar answered Oct 04 '22 09:10

Jens Schauder