CrudRepository in Spring Data
CrudRepository provides standard create, read, update, and delete operations for an entity through a repository interface. It lets a Spring Data application perform common persistence tasks without writing the usual repository implementation code.
`CrudRepository<T, ID>` is a Spring Data repository abstraction where `T` is the entity type and `ID` is the type of that entity's identifier. A repository interface can extend it to inherit common persistence methods.
Typical operations include `save`, `findById`, `findAll`, `existsById`, `count`, `deleteById`, and `delete`. The exact available methods depend on the Spring Data version being used, so interview answers should focus on the abstraction rather than memorizing every method signature.
`save` is used for persistence of an entity and can cover both creating a new entity and updating an existing one, depending on the persistence technology and entity state. `findById` returns an `Optional<T>`, encouraging the caller to handle the case where no entity exists.
CrudRepository is an interface, so application code normally declares a repository interface that extends it. Spring Data then provides the implementation at runtime, allowing application code to inject and use the repository without manually implementing CRUD logic.
Key points
- `CrudRepository<T, ID>` is parameterized by entity type and identifier type.
- It supplies standard CRUD persistence operations.
- `findById` returns an `Optional<T>`.
- Spring Data creates the repository implementation at runtime.
- Custom query methods can be added to a repository interface when CRUD operations are insufficient.
Interview questions
What is CrudRepository in Spring Data?
CrudRepository is a Spring Data interface that defines common create, read, update, and delete operations for an entity. A developer typically creates an interface that extends CrudRepository and specifies the entity and ID types. Spring Data supplies the implementation automatically at runtime. This avoids writing repetitive repository implementation code.
What do the generic parameters in CrudRepository mean?
The first generic parameter identifies the entity managed by the repository. The second identifies the type of that entity's primary identifier. For example, `CrudRepository<User, Long>` represents a repository for `User` entities whose IDs are `Long` values. These types allow repository methods to expose type-safe parameters and return values.
What is the difference between save and findById?
`save` is used to persist an entity and may represent either creating or updating data depending on the entity's state and persistence provider. `findById` retrieves an entity using its identifier. `findById` returns an `Optional`, so the caller must account for the possibility that no matching entity exists. The methods therefore serve different directions of the persistence interaction.
Does CrudRepository only work with relational databases?
CrudRepository is a Spring Data Commons abstraction rather than a SQL-only API. Different Spring Data modules can provide repository implementations for different persistence technologies. For example, Spring Data JPA uses repository abstractions for JPA-backed relational persistence. Therefore, the repository interface describes common data-access behavior while the specific Spring Data module supplies the technology-specific implementation.
When might you choose JpaRepository instead of CrudRepository?
The choice depends on the capabilities the application needs and the persistence technology. CrudRepository is appropriate when the application only needs the basic CRUD contract. JpaRepository is JPA-specific and exposes additional functionality beyond the basic CRUD abstraction. The important distinction is to select the smallest repository abstraction that provides the required behavior rather than choosing solely by habit.