JPA vs Hibernate vs Spring Data JPA

History

The developer community realize that a simpler, lightweight, and POJO based framework could ease the development of data access logic. Since then, many different libraries have appeared; they are generally referred to as ORM libraries.

The main objective of an ORM library is to close the gap between the relational data structure in the RDBMS and the OO model in Java so that developers can focus on programming with the object model and at the same time easily perform actions related to persistence.

JCP developed the Java Data Objects (JDO) specification as one of the standard ORM technologies in Java EE. Starting from EJB 3.0, the EJB entity bean was even replaced with the Java Persistence API (JPA), within which a lot of the ideas were influenced by popular ORM libraries such as Hibernate, TopLink, and JDO.

The relationship between Hibernate and JPA is very close. Gavin King, the founder of Hibernate, represented JBoss as one of the JCP expert group members in defining the JPA specification.

Starting from version 3.2, Hibernate has provided an implementation of JPA. So, when you develop applications with Hibernate, you can choose to use either Hibernate’s own API or the JPA with Hibernate as the persistence service provider.

Key Differences

Aspect JPA Hibernate Spring Data JPA
Type Specification Implementation Abstraction layer
Dependency Independent of providers Specific library Built on JPA (uses providers like Hibernate)
Purpose Standard ORM API Full-featured ORM solution Simplified data access
Features Basic ORM Advanced ORM features Auto-repository generation

JPA (Java Persistence API)

What it JPA?

JPA is a specification for object-relational mapping (ORM) in Java.

It defines a standard way to map Java objects to database tables and manage relational data using an ORM approach. JPA can be thought of as a contract or blueprint for how Java objects can be mapped to database tables.

It does not provide an actual implementation; instead, it defines interfaces and annotations that need to be implemented by a provider (e.g., Hibernate).

Role in application:

Serves as an abstraction layer, allowing developers to use standardized APIs without depending on a specific ORM implementation.

Key Point about JPA:

It provides a standard set of annotations like @Entity, @Table, @Column, and @Id to map Java objects to database tables.

It defines interfaces like EntityManager, Query, and Transaction, which are implemented by ORM providers like Hibernate.

It makes your code portable. You can switch from Hibernate to another JPA provider (e.g., EclipseLink) without changing much of your code.

Example: Writing portable code that works with any JPA-compliant provider.

Hibernate

What it Hibernate?

Hibernate is a JPA implementation (and much more). It’s a popular ORM tool that implements the JPA specification and works under the hood to interact with the database.

Hibernate looks at the annotations defined in your JPA entity (like @Entity, @Id) and generates the SQL to create, read, update, or delete rows in the database.

It manages connections, transactions, and database interaction for you

Role in application:

Acts as the engine that executes the ORM logic defined by JPA.

Offers advanced features not included in the JPA spec (e.g., native Hibernate Query Language (HQL), interceptors, and custom caching).

Key Features of Hibernate:

Supports both JPA APIs (standardized) and its own native APIs (e.g., HQL, caching).

Provides advanced features like caching, batching, and custom queries.

Handles database-specific optimizations.

Example:

If you write code using JPA’s EntityManager, Hibernate processes these instructions under the hood.

Spring Data JPA

What is Spring Data JPA?

Spring Data JPA is a Spring module built on top of JPA and Hibernate (or another JPA provider). It simplifies data access layers by abstracting common boilerplate code (like repository patterns) and offering additional features like query creation from method names.

Instead of manually creating an EntityManager or writing query code, you just define a repository interface. Spring generates the implementation at runtime.

Role in application:

Simplifies the creation and management of JPA repositories.

Provides pre-built repository interfaces like CrudRepository and JpaRepository interfaces for basic CRUD operations.

Automatically generates queries based on method names (e.g., findByName). Support for custom queries via @Query.

You focus on defining interfaces and method names, while Spring Data JPA generates the implementation automatically.

Example:

Here’s how you’d use Spring Data JPA to fetch user data:

public interface UserRepository extends JpaRepository<User, Long> {
    User findByUsername(String username);
}