In Spring Data, different repositories provide abstraction for various data storage solutions. The main repositories used are:
- CRUD Repository → Provides basic CRUD operations.
- JPA Repository → Extends CRUDRepository, provides additional JPA-specific methods.
- Mongo Repository → Used for MongoDB, extending
CrudRepository
with MongoDB-specific features.
CRUD Repository
CrudRepository<T, ID>
is the base interface in Spring Data that provides basic CRUD (Create, Read, Update, Delete) operations.
Features
- Supports generic CRUD operations (
save()
,findById()
,delete()
, etc.). - Works with relational (JPA) and NoSQL (MongoDB, Cassandra, etc.) databases.
- Requires minimal boilerplate code.
Example
public interface EmployeeRepository extends CrudRepository<Employee, Long> { }
Available Methods
Optional<T> findById(ID id); void deleteById(ID id); void delete(T entity); <S extends T> S save(S entity); Iterable<T> findAll(); long count();
2. JPA Repository (For Relational Databases)
JpaRepository<T, ID>
extends CrudRepository<T, ID>
and PagingAndSortingRepository<T, ID>
, adding extra methods for JPA-based operations.
Features
- Pagination & Sorting (
findAll(Pageable p)
,findAll(Sort s)
). - Batch operations (
saveAll()
for bulk insert/update). - Flush & Delete methods (
deleteInBatch()
,flush()
). - Supports JPQL and native queries.
Example
public interface EmployeeRepository extends JpaRepository<Employee, Long> { }
Available Methods
In addition to CrudRepository
methods:
List<T> findAll(); List<T> findAll(Sort sort); Page<T> findAll(Pageable pageable); <S extends T> List<S> saveAll(Iterable<S> entities); void flush(); <S extends T> S saveAndFlush(S entity); void deleteInBatch(Iterable<T> entities);
Use Case:
For relational databases like MySQL, PostgreSQL, Oracle using Hibernate as JPA provider.
Mongo Repository (For MongoDB – NoSQL)
MongoRepository<T, ID>
extends CrudRepository<T, ID>
, providing MongoDB-specific features.
Features
- Works with MongoDB (NoSQL), supports BSON documents instead of relational tables.
- Supports aggregation operations (
findBy...()
,deleteBy...()
). - Uses MongoDB Query Language (not JPQL).
- Provides custom queries using JSON-style query DSL.
Example
public interface EmployeeRepository extends MongoRepository<Employee, String> { }
Available Methods
Same as CrudRepository
, but optimized for MongoDB:
List<T> findAll(); List<T> findByDepartment(String department); List<T> findByAgeBetween(int start, int end); long count();
Use Case:
For MongoDB NoSQL databases, storing data as JSON-like BSON documents instead of relational tables.
Comparison Table
Feature | CrudRepository | JpaRepository | MongoRepository |
---|---|---|---|
Database Type | Any (SQL & NoSQL) | Relational (JPA) | NoSQL (MongoDB) |
Basic CRUD | ✅ Yes | ✅ Yes | ✅ Yes |
Pagination & Sorting | ❌ No | ✅ Yes (findAll(Pageable) ) | ✅ Yes (findAll(Pageable) ) |
Batch Processing | ❌ No | ✅ Yes (saveAll() ) | ✅ Yes (saveAll() ) |
Native Queries | ❌ No | ✅ Yes (@Query ) | ✅ Yes (@Query ) |
Flush & Batch Delete | ❌ No | ✅ Yes (flush() , deleteInBatch() ) | ❌ No |
Use Case | Basic CRUD | Relational Databases (MySQL, PostgreSQL, etc.) | MongoDB |
Which Repository to Use?
- Use CrudRepository when you only need basic CRUD operations.
- Use JpaRepository when working with SQL databases and need pagination, sorting, and batch processing.
- Use MongoRepository when working with MongoDB (NoSQL databases).