50

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.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
  • Did you refer http://stackoverflow.com/questions/28163670/jdbc-vs-hibernate ?? – Akshay Feb 26 '17 at 15:07
  • Hi @Akshay , I was specifically looking for cases where Spring Data JDBC & Spring Data JPA. – Punter Vicky Feb 26 '17 at 15:10
  • 1
    Why choose? Why not simply use Spring Data JPA with Hibernate as the JPA provider...? – Florian Schaetz Feb 26 '17 at 16:59
  • 1
    Thanks @FlorianSchaetz , is there an advantage of using spring data jpa along with hibernate ? Also would I lose anything if I just use spring data jdbc? – Punter Vicky Feb 26 '17 at 17:07
  • No discussion about ORMs is complete without this: http://blogs.tedneward.com/post/the-vietnam-of-computer-science/ – Jens Schauder Feb 27 '17 at 14:46
  • JDBC means doing stuff manually. Spring makes it slightly easier. JPA allows you much more. it simply depends on your use case. For many typical applications, JPA will be much easier. – Florian Schaetz Mar 01 '17 at 08:04

4 Answers4

101

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.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • 7
    If you have to ask yourself "Do I need the fine grained control via JDBC", the you probably do not need it. If you needed it, you would know, because you would be knee deep in it already. – Florian Schaetz Mar 01 '17 at 08:04
  • Spring Data is one of THE best invention for developers for data management. I always said: Using pure JDCB is pointless, same as using pure JPA. You have to know when to mix them. If you are using Java and not using Spring Data, you should start to. – Awi Sep 24 '18 at 14:18
  • Great Answer, however I can't tell the advantages of using Spring Data JPA over Spring Data JDBC or vice versa. Could you clear that ? I honestly don't see how to judge if I want to use one over the other in new project. It seems I have to search CrudRepository vs JpaRepository after all ! – Anddo Oct 23 '18 at 21:37
  • 1
    @Anddo I'd say the question is: do you profit from the features of JPA, or are they getting in your way. Spring Data JDBC is much simpler: fewer features but also way easier to understand. – Jens Schauder Oct 24 '18 at 05:04
  • 1
    @Anddo: If you are looking for an evaluation between these two wonderful frameworks, please do refer this [link](https://medium.com/@maqbool.ahmed.mca/spring-data-jpa-vs-data-jdbc-evaluation-b36d8834ead6) – Maqbool Ahmed May 10 '20 at 07:10
  • @MaqboolAhmed Thanks. Very interesting read. I do agree making things under your control is really recommended and really saves a lot of effort and time on the long run but JPA solved too many problems. Specially, lazy loading and mapping to objects part. Since my comment, I decided to go with JPA with the great headache I suffer from until now, to care a lot about the details and very explicit queries for my dao layer. Thanks to Spring Data, this is now very very simplified. – Anddo May 10 '20 at 12:15
  • 1
    @Anddo Glad you found it interesting. Totally agreed that JPA solves lot of problems and what to use depends on a lot of factors and it's totally a team's choice. The sole intention of doing this exercise is to have these numbers infront of us while taking such difficult decisions. It will be interesting to take a small set of challenges you are facing and see how Spring Data JDBC addresses them, would you be in a similar situation of headache :). If you don't mind do send them to me. Btw, thanks to JensSchauder and team, you have done a fabulous job, hoping to see a lot of good work. – Maqbool Ahmed May 10 '20 at 23:23
18

One problem with your question is you seem to imply that Spring Data JPA is like Hibernate and that's actually not true. Spring Data JPA is merely a spring-centric wrapper that offers springy semantics and features that wrap a JPA provider of which Hibernate is one implementation.

Ergo, you cannot use Spring Data JPA without including some JPA implementation like Hibernate.

The base question you're asking then is why use JDBC vs an ORM. In order to understand that you should take time to understand the benefits of an ORM. There are numerous articles on the internet that can give you that.

But even in an ORM driven application, there will be times where you'll have cases that you need to bypass the ORM framework and use native SQL just like you would in JDBC. Those cases are often rare, but necessary when you want to take advantage of some unsupported database feature or where you want to have ultimate control of manipulating the results, etc.

The deciding factor on which to use ultimately depends on your application's needs. But just because you elect to use an ORM framework doesn't mutually exclude the ability to execute native queries and SQL statements like you would in JDBC. Those features are still available, you just typically use those in rare cases.

Naros
  • 19,928
  • 3
  • 41
  • 71
  • Thanks Naros , this is helpful. What is the advantage of using spring data in both jdbc and orm scenarios ? – Punter Vicky Feb 26 '17 at 19:29
  • As Jens pointed out, spring data jdbc doesn't officically exist, so none. Spring Data JPA makes using ORM another notch easier, which is basically what Spring does: Take stuff and make them easier to use. – Florian Schaetz Mar 01 '17 at 08:06
  • +1 for "Spring Data JPA is merely a spring-centric wrapper that offers springy semantics and features that wrap a JPA provider". If you trace through the dependencies of the spring-boot-starter-data-jpa POM file you will see that one of them is org.hibernate.hibernate-core. – Night Owl Jun 30 '17 at 22:58
  • 2
    This answer is outdated and no longer correct. Spring data jdbc exists and is an ORM in and of itself. – Oleg Aug 03 '20 at 13:40
3

The thing is Spring data for jpa is an abstraction over jpa, which is an abstraction over JDBC. If offer nice features, even if jpa is usable without it. It becomes really powerful with Spring data rest.

But the more a framework does for you, the more you need to understand underlying technology. It is even more true if you use spring-data-rest. The best is to start by understanding sql (design and queries), then jpa (lazy loading, instance states, entities, embeddables, caches, queries, transaction synchronization).

Then you try spring-data-jpa and decide if it brings value to you project. For save, update, delete and find by id operations it is basically a wrapper around persist, merge, remove and find method of EntityManager. The main advantage of Spring data JPA is the query support, but querydsl is also a nice option. In both case it requires a clear understanding of how JPA works. The first thing to do is to turn on SQL logging to see if, for a given db access, your jpa implementation execute queries that a dba would consider correct. For instance, eager loading on an entity which is not in cache results in 1+n select, you unit test will pass and problems will start in production. spring-data-jpa will not solve the problem for you.

There are many spring-data-* beside spring-data-jpa, some of them also offer mapping annotations (spring-data-cassandra for instance), which is useful when there is no object-db mapping.

Gengis
  • 31
  • 1
  • 2
2

I was looking for the answer for this. The following comparison is taken from Java Persistent with Spring Data and Hibernate.

The point that I can take is that there could be a situation where a custom functionality that needs to tinker around with database-specific functionality that Spring Data JPA provided. Or perhaps, we want to avoid such a complexity that is brought over by spring data magic.

Spring Data JPA Spring Data JDBC
Database independent and portable Generally, database specific
Introduces complexity through Object-Relational Mapping Less complex, still adheres to the Spring framework principles
Automatic schema generation based on the entities Schema generation through DDL commands on the side of the programmer
Query derivation since the first version Query derivation since version 2.0
Query annotated with JPQL code and native SQL Only queries using native SQL
May reuse classes with JPA annotations. Uses the annotations from the org.springframework.data package
Models relationships between entities through annotations as @OneToMany, @Embedded, etc. Modeling the relationships mostly on the side of the programmer, through the design of the classes.
Caching and lazy loading No caching, no lazy loading
Sessions and dirty tracking No sessions, no dirty tracking
alfonzjanfrithz
  • 716
  • 7
  • 16