liveBook · Manning

CrudRepository

As explained in Java Persistence with Spring Data and Hibernate epub

CrudRepository is a Spring Data repository interface providing generic create, read, update, and delete operations for an entity and its identifier type.

CrudRepository is a generic Spring Data persistence interface parameterized with an entity type and its identifier type. Extending it lets an application use inherited operations such as save, findAll, and findById without implementing repository methods or directly managing transactions. Spring Data creates a proxy that implements the application’s repository interface. section 2.6 section 15.1

A repository declaration identifies the model and key types:

  • In JPA, MessageRepository extends CrudRepository<Message, Long>; Message uses a Long field annotated with @Id. section 2.6
  • In Spring Data JDBC, UserRepository extends CrudRepository<User, Long>, with findAll overridden to return List<User> instead of Iterable<User>. section 15.1
  • The same repository style supports Spring Data JPA and Spring Data JDBC, while mapping annotations and configuration differ between them. section 2.6 section 15.1

Minimal repository and use

public interface MessageRepository extends CrudRepository<Message, Long> { } Message message = new Message(); message.setText("Hello World from Spring Data JPA!"); messageRepository.save(message); List<Message> messages = (List<Message>) messageRepository.findAll(); The repository is injected into the test, so explicit object creation for repository infrastructure and explicit transaction control are unnecessary. section 2.6

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> {
 
}

Inspect the complete empty MessageRepository declaration used with Spring Data JPA. section 2.6

Listing 15.5 The UserRepository interface
Path: Ch15/spring-data-jdbc/src/main/java/com/manning/javapersistence/ch15
 /repositories/UserRepository.java
 
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
    List<User> findAll();
}

Inspect the Spring Data JDBC repository declaration, including the List<User> findAll override. section 15.1

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Defines generic parameters, inherited methods, proxy generation, injection, and configuration implications without detailing proxy internals.
Examples
Uses Message and User repositories with save, saveAll, findAll, findById, and deleteAll in configured database tests.
What it assumes
Assumes entity classes, identifier fields, database mapping, and Spring configuration; teaches repository declarations from their minimal interface form.
What you can do after
Apply it via Spring Data JPA or Spring Data JDBC to define injectable repositories with standard persistence operations.
How it connects
Connects entity identifiers and repository interfaces with Spring Data JPA, Spring Data JDBC, proxy classes, and relational database configuration.

Also explained in

Compare how 2 books explain CrudRepository →

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.