CrudRepository is a Spring Data interface that gives you ready-made create, read, update and delete methods for an entity without writing any implementation. You extend it with your entity type and id type, and Spring Data generates a working repository bean at startup.
It is the simplest repository abstraction in Spring Data and works with JPA, MongoDB, JDBC, Redis and other modules. For paging, sorting or JPA-specific helpers you extend a richer interface that builds on it.
The interface is declared as CrudRepository<T, ID>, where T is the entity and ID is the type of its identifier. Its core methods are:
| Method | Purpose |
|---|---|
save(S entity) | Inserts a new entity or updates an existing one |
saveAll(Iterable<S>) | Saves several entities |
findById(ID id) | Returns an Optional<T> |
existsById(ID id) | Checks whether an entity exists |
findAll() | Returns all entities as an Iterable<T> |
findAllById(Iterable<ID>) | Returns entities for the given ids |
count() | Returns the number of entities |
deleteById(ID id) | Deletes by id |
delete(T entity) | Deletes the given entity |
deleteAll() and deleteAllById(...) | Bulk deletes |
Since Spring Data 3.0 the Kotlin-style and list-based variants live in ListCrudRepository, which returns List instead of Iterable.
Define the entity and the interface, then inject the repository where needed.
With Spring Boot you do not need @Repository on the interface or @EnableJpaRepositories; auto-configuration scans for repository interfaces. The repository methods are transactional by default, with read-only transactions for the query methods.
save checks whether the entity is new. With JPA, an entity is new when its id is null (or, for primitive ids, zero), or when a @Version attribute is null. A new entity is persisted, otherwise it is merged. Always use the returned instance, because merge can return a different object than the one passed in.
If you assign ids yourself, implement Persistable or add a @Version field, otherwise Spring Data may issue an unnecessary SELECT before every insert.
You extend the interface with derived queries by following a naming convention, or write explicit queries with @Query.
Spring Data parses the method names at startup, so a typo in a property name fails fast with a clear error.
| Interface | Adds | Use when |
|---|---|---|
CrudRepository | Basic CRUD | Simple data access, or non-JPA stores |
PagingAndSortingRepository | findAll(Sort) and findAll(Pageable) | You need paging, but not CRUD in the same interface (since Spring Data 3 it no longer extends CrudRepository) |
ListCrudRepository | CRUD with List results | You prefer lists over Iterable |
JpaRepository | Flush, batch deletes, List returns, plus paging | Most JPA applications |
In Spring Data 3 and later, PagingAndSortingRepository no longer extends CrudRepository, so combine the interfaces explicitly or extend JpaRepository, which includes both.
findAll() on large tables. Use Pageable instead.findById over getReferenceById unless you want a lazy proxy.deleteAllInBatch (JPA) when you need a single SQL delete rather than loading each entity.No. Spring Data creates a proxy implementation at runtime, backed by SimpleJpaRepository for JPA.
JpaRepository extends the repository hierarchy with JPA-specific operations such as flush, batch deletes and List return types. CrudRepository is store-neutral.
No. It inserts new entities and merges existing ones, which usually triggers a SELECT followed by an UPDATE.
No. It returns an empty Optional.
CrudRepository removes boilerplate by giving every Spring Data repository standard CRUD methods. Extend it with your entity and id types, add derived or @Query methods as needed, and choose JpaRepository when you need paging, flushing and list results.