liveBook · Manning

CrudRepository

As explained in Spring in Action, Sixth Edition

CrudRepository is a Spring Data repository interface that provides generic create, read, update, and delete operations for persistent domain objects.

CrudRepository is a Spring Data interface for common create, read, update, and delete operations. Instead of writing a persistence class, declare an interface that extends CrudRepository. Spring Data creates its implementation when the application starts. section 3.2.2

Listing 3.13 Defining a repository interface for persisting ingredients
package tacos.data;
 
import org.springframework.data.repository.CrudRepository;
 
import tacos.Ingredient;
 
public interface IngredientRepository 
         extends CrudRepository<Ingredient, String> {
 
}

IngredientRepository needs no method declarations to gain the standard operations. In CrudRepository<Ingredient, String>, Ingredient is the object being stored and String is its ID type. section 3.2.2

An order repository follows the same pattern: CrudRepository<TacoOrder, Long>. Prepare the domain objects for persistence, including their IDs, then inject the repositories where the application needs them. This replaces explicit JdbcTemplate repository implementations while providing additional standard operations. section 3.2.2 section 3.2.3

Add a query beyond basic CRUD

Declare List<TacoOrder> findByDeliveryZip(String deliveryZip); in OrderRepository. Spring Data reads the method name as a request to find TacoOrder objects whose deliveryZip property matches the argument and generates the query implementation. section 3.3.4

The interface pattern carries across Spring Data JDBC, JPA, Cassandra, and MongoDB. Check the ID type when changing databases: the order example uses Long with JPA, UUID with Cassandra, and String with MongoDB. Domain mapping can change even when the repository interface barely does. section 4.1.4 section 4.2.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Shows complete interface declarations, type parameters, generated behavior, and a custom query declaration; does not detail CrudRepository method implementations.
Examples
Uses ingredient and order repositories, a delivery-ZIP query, and database-specific order ID types.
What it assumes
Introduces repository type parameters directly while assuming Java interfaces and persistent domain objects.
What you can do after
Apply it via Spring Data to declare repositories with standard operations and add a derived query when needed.
How it connects
Connects repository interfaces with domain IDs, Spring Data JDBC, JPA, Cassandra, MongoDB, and derived query methods.

Also explained in

Compare how 2 books explain CrudRepository →

Related concepts

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