liveBook · Manning

Spring Data JPA

As explained in Java Persistence with Spring Data and Hibernate epub

Spring Data JPA is a Spring Data module that adds repository and query-generation support on top of JPA implementations.

Spring Data JPA adds a repository layer over JPA implementations such as Hibernate. JPA still defines the entity mappings; Spring Data JPA reduces data-access boilerplate by creating repository implementations and generating queries from method names. It does not remove the need to understand the SQL executed by the underlying provider. section 1.3 section 2.2

A basic persistence workflow is:

  • Define and map an entity, including its identifier. section 2.6 section 2.8
  • Declare an interface such as `MessageRepository extends CrudRepository<Message, Long>`; Spring Data JPA supplies a proxy implementation with inherited methods including `save`, `findAll`, and `findById`. section 2.6
  • Configure the data source, entity manager factory, and transaction manager. Spring Data JPA supports annotation-based transaction management; configuration takes more effort up front than the short repository-using test suggests. section 4.1 section 2.6

Store and retrieve a message

Inject `MessageRepository`, create a `Message`, call `messageRepository.save(message)`, then retrieve records with `messageRepository.findAll()`. The repository-using test needs no explicit object creation for the repository or explicit transaction control. This is an alternative to calling JPA’s `EntityManager` or native Hibernate’s `Session` directly, not a replacement for entity mapping. section 2.6 section 2.8

Listing 2.11 The MessageRepository interface
Path: Ch02/helloworld/src/main/java/com/manning/javapersistence/ch02
 /repositories/MessageRepository.java
 
public interface MessageRepository extends CrudRepository<Message, Long> {
 
}

The interface declaration shows the entity and identifier types; its body is empty because the standard repository operations come from `CrudRepository`. section 2.6

For a Spring Boot project, Spring Initializr can add `spring-boot-starter-data-jpa` alongside a MySQL driver. Spring Data JPA can also underpin a Spring Data REST application whose repositories are exposed as RESTful resources. Compare it with direct JPA or Hibernate access according to dependencies, code volume, portability, and execution speed rather than treating any option as universally better. section 4.2 section 16.2 section 2.8

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Works through configuration, a repository interface, proxy-generated methods, and a save-and-load test; retains JPA entity mapping.
Examples
Uses a message repository that saves and retrieves an entity, plus Spring Boot projects backed by MySQL.
What it assumes
Assumes familiarity with relational modeling and SQL; introduces repository declaration and configuration through a first application.
What you can do after
Apply it via Spring Data JPA to configure a project, declare a repository, and store and load mapped entities.
How it connects
Connects JPA, Hibernate, Spring Data Commons, repositories, transaction management, Spring Boot, and Spring Data REST.

Also explained in

Compare how 2 books explain Spring Data JPA →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.