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
Inspect the complete empty MessageRepository declaration used with Spring Data JPA. section 2.6
Inspect the Spring Data JDBC repository declaration, including the List<User> findAll override. section 15.1

