liveBook · Manning

CrudRepository

As explained in Spring Start Here, Second Edition

CrudRepository is a Spring Data repository interface that declares common create, retrieve, update, and delete operations for a model type.

CrudRepository is a Spring Data interface for basic persistence operations: creating, retrieving, updating, and deleting records. Extend it when an application needs those operations without declaring each one separately. section 14.2 section 14.5

A repository interface supplies two generic types: the model class and the type of its primary key. In Spring Data JDBC, mark the model’s primary key field with @Id so Spring Data can identify it. section 14.3

Listing 14.4 Defining the Spring Data repository
public interface AccountRepository 
    extends CrudRepository<Account, Long> {
 
}

AccountRepository inherits CRUD operations for Account records, whose primary key type is Long. section 14.3

Spring Data provides a bean implementing the repository contract. Other components can inject it: for example, a transfer service can call findById to retrieve an account or findAll to retrieve all accounts. section 14.2 section 14.3

Choose a contract that fits the operations you need:

  • Repository is a marker interface and declares no persistence operations. section 14.2
  • CrudRepository adds basic CRUD operations. section 14.2
  • PagingAndSortingRepository adds operations for sorting and retrieving records in pages. section 14.2

Inherited operations do not cover every query an application needs. A repository can declare a custom method such as findAccountsByName; Spring Data can derive its query from the method name. For more complex operations, use @Query to specify the query explicitly. Modifying operations also need @Modifying. section 14.3 section 14.5

Listing 14.5 Adding a repository operation to get all the accounts with a specified name
public interface AccountRepository 
  extends CrudRepository<Account, Long> {
 
  List<Account> findAccountsByName(String name);
  
}

This custom method adds a name-based search alongside the inherited CRUD operations. section 14.3

How Spring Start Here, Second Edition covers it

How deep it goes
Explains interface inheritance, generic types, and custom operations, without detailing the repository implementation internals.
Examples
Uses an account repository and a money-transfer service to demonstrate inherited retrieval and custom name-based queries.
What it assumes
Uses a model class with an @Id-marked primary key before defining the repository.
What you can do after
Apply it via Spring Data JDBC to define a repository and use inherited and custom persistence operations.
How it connects
Connects repository interfaces with dependency injection, primary keys, query derivation, pagination, and sorting.

Also explained in

Compare how 3 books explain CrudRepository →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.