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
