Spring Data JPA vs Hibernate

Spring Data JPA

Spring Data JPA Tutorial

Spring Data JPA Example

Spring Data JPA CRUD Example

Spring Data JPA vs Hibernate

Spring Data JPA Interview Questions

What is Hibernate?

Hibernate is a JPA provider. This means it provides an implementation for the JPA specification.

JPA Specification???

The Java Persistence API (JPA) is a specification for mapping Java objects to database tables. Annotations like @Entity point to JPA provider (Hibernate) implementations of JPA specifications. AKA JPA specifies what an Entity is and Hibernate provides the implementation for that interface or specification.

This sounds a bit confusing because JPA originated from ORMs like Hibernate. JPA standardizes how ORM tools should look and behave.

What is Spring Data JPA

Spring Data JPA is an abstraction that makes it easier to work with a JPA provider. Specifically Spring Data JPA provides a set of interfaces for easily creating data access repositories.

Spring Data JPA vs Hibernate: The Key Difference

Consider the following implementation of a CrudRepository using Spring Data JPA:

package bookservice.repository;

import bookservice.model.Author;
import org.springframework.data.repository.CrudRepository;

public interface AuthorRepository extends CrudRepository {

By simply extending the CrudRepository interface, Spring will automagically implement the following repository methods:


making it super easy to persist objects to the database...

//create new author
Author author = new Author();
//save to database
//find all authors
List authors = repo.findAll();

If we didn't use Spring Data JPA, we'd have to write more boilerplate code to persist entities to the database...

EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "Hibernate" );

EntityManager entitymanager = emfactory.createEntityManager( );
entitymanager.getTransaction( ).begin( );

Author author = new Author( );

entitymanager.persist( author );
entitymanager.getTransaction( ).commit( );

entitymanager.close( );
emfactory.close( );

Notice the verbosity involved with the entity manager, committing and closing transactions? This is the type of code Spring Data JPA helps to avoid.

Spring Data JPA is really a set of dependencies that makes it easier to work with a JPA provider. Hibernate is one of several JPA providers. This means you can use Spring Data JPA without using Hibernate (if you really wanted to).


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.

Your thoughts?


Spring Data JPA all the way