Spring + hibernate versus Spring Data JPA: Are they different?
Asked Answered
P

4

53

Although not novice, I am trying to learn spring framework (again!) in order to be sure that I really understand this. I have got fair idea on core Spring (DI). Now, I am focusing on Data layer.

I have come across the term "Spring and Hibernate". As I can interpret it would mean using Spring Framework with Hibernate as ORM tool/JPA provider.

Now I have come across "Spring Data JPA". I clarified on SO about Spring Data JPA, that it is an abstraction layer on-top of JPA (and under the hood Spring Data JPA uses Hibernate or any other JPA provider).

Now are these terms same? That is, is "Spring + hibernate" same as that of "Spring Data JPA". If not, then what is the difference / similarities?

I am really confused on so many terms/statements (like above) seemingly to be similar, but may be different.

Peace answered 3/5, 2016 at 19:7 Comment(1)
Possible duplicate of Spring Data-JPA versus JPA: What's the difference?Tootle
R
65

Spring-data-jpa, as you're saying, offers more that just the classical Spring-JPA integration. With JPA/Hibernate integration, you get mainly

  • declarative transaction management using JPA/Hibernate transactions
  • exception translation
  • the JPA EntityManager or the Hibernate SessionFactory as injectable beans

With Spring-data-jpa, you get all that, plus (among other things)

  • interface-only repositories, using method names to infer queries automatically
  • @Query annotation to define the query that an interface method should return
  • automatic handling of Pageable queries
  • base classes for standard crud repositories

This is just a tiny introduction. For more help, read the documentation.

Roundshouldered answered 3/5, 2016 at 19:15 Comment(3)
Thanks so much for your reply. So when we see "Spring + Hibernate" it means Spring using Hibernate directly, whereas, when we see "Spring Data JPA" it mean using spring data jpa abstraction and jpa provider can be anything (like Hibernate, eclipselink etc,) And Spring data jpa has more to offer, few of which you already mentioned in your reply.Peace
Yes. But you can also use Spring with any JPA provider, without using Spring-data-jpa.Roundshouldered
Thanks a lot for clarifying this doubt. Appreciate a lot your time.Peace
H
8

when you talk about spring + hibernate

  1. If you read hibernate alone, you would understand it uses mapping(which basically configuration for mapping of pojo with database relations) and configuration(configuration specific to database like driver class, url, username, password, dialect etc.).
  2. So now if you want to use read, write, update etc. you have to get hibernatesessionfactory, open transaction and commit. Lot of pre and post work for each operation.
  3. When you integrate hibernate with spring, spring uses this configuration and keep it in application context, and provide wrapper which is hibernatetemplate which internally using hibernatesessionfactory. So you dont need to care much about pre and post code while doing these operations.
  4. It also provided caching(first and second level cache) to improve performance.
  5. And also give you to work with HQL which is independent of database.It uses database dialect to generate database specific sql.

Now lets talk about spring + data jpa

  1. it comes with base repository interfaces(from spring data common, spring jpa)
  2. So suppose you are interested in doing crud operation, just extend crud repository, and spring will inject its implementation at run time.
  3. Lets say you want to define common method for your application, you can do that by creating new repository interface which extends Repository interface. And can use this across the application.
  4. It also provide query methods which allow you to use native sql or jpql.
Heliostat answered 1/8, 2017 at 11:30 Comment(0)
P
3

Spring Data JPA is not a JPA provider. It is a library/framework that adds an extra layer of abstraction on the top of our JPA provider (like Hibernate).

So you can't use Spring Data without a JPA provider like Hibernate, EclipseLink, etc.

Pratique answered 21/8, 2020 at 14:32 Comment(0)
P
2

I find this link helpful:

JPA is the Java Persistence API, which is Java's standard API for object-relational mapping.

JPA is only an specification - you need an implementation of it to be able to use it. Hibernate is one of the most well-known and most used implementations of JPA, but there are others, such as EclipseLink JPA.

The Spring Framework is a large framework to help you write enterprise-grade software easier. It contains support for many Java technologies, including JPA.

The Spring Framework consists of a collection of projects, and one of these projects is Spring Data.

The goal of Spring Data is to make it easier to work with different kinds of databases, from traditional relational databases to NoSQL databases. Spring Data supports JPA via the Spring Data JPA subproject.

To write a program that uses JPA, you need at least a JPA implementation, such as Hibernate.

If you are using the Spring Framework for your application, you will most likely want to use Spring Data JPA together with Hibernate.

Propaganda answered 20/3, 2018 at 5:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.