liveBook · Manning

JpaRepository

As explained in Java Persistence with Spring Data and Hibernate epub

JpaRepository is the Spring Data JPA repository interface that provides standard persistence operations and query-method support for JPA entities.

JpaRepository is a Spring Data JPA interface for accessing JPA entities. Declare a repository interface for an entity and its ID type; Spring Data JPA creates the implementation. This replaces handwritten DAO implementations for standard operations and supports queries derived from method names. section 1.3

public interface UserRepository extends JpaRepository<User, Long> {
}
 
public interface LogRepository extends JpaRepository<Log, Long> {
 
}

The two declarations show the pattern: UserRepository and LogRepository each extend JpaRepository with their entity type and Long ID type, without supplying an implementation class. section 20.2

JpaRepository extends PagingAndSortingRepository, which extends CrudRepository. CrudRepository supplies basic create, read, update, and delete operations; PagingAndSortingRepository adds sorting and pagination. JpaRepository adds JPA-related operations, including flushing the persistence context and batch deletion. Its findAll, findAllById, and saveAll methods return List rather than Iterable. section 4.4

Declare retrieval methods

A UserRepository extending JpaRepository<User, Long> can declare User findByUsername(String username), List<User> findByRegistrationDateBetween(LocalDate start, LocalDate end), and List<User> findByLevelOrderByUsernameDesc(int level). Spring Data JPA parses the names to produce retrieval queries: the method name supplies the property, condition, or ordering instead of requiring a handwritten query. section 4.4

Choose JpaRepository when these JPA-specific operations or List return types are useful. If basic CRUD is sufficient, CrudRepository offers a narrower interface; Spring Data JDBC is an alternative when direct JDBC access is preferable to Spring Data JPA’s generated behavior. section 4.4 section 1.3

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains interface inheritance, return types, JPA-specific methods, and name-derived queries, but does not detail generated implementation internals.
Examples
Shows UserRepository query methods for usernames, date ranges, and ordering, alongside separate User and Log repositories.
What it assumes
Assumes familiarity with Java interfaces and introduces the repository pattern through JPA entity and ID type declarations.
What you can do after
Apply it via Spring Data JPA to declare entity repositories and retrieval methods without implementing standard operations or those queries.
How it connects
Connects JpaRepository to JPA entities, CrudRepository, PagingAndSortingRepository, persistence contexts, and derived queries.

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.