![]() |
VOOZH | about |
In Spring Boot CrudRepository and JpaRepository are used to perform database operations in applications built with Spring Data JPA. While CrudRepository provides basic CRUD functionalities, JpaRepository extends it and offers additional features like pagination, sorting, and batch operations, making it more powerful for advanced data handling.
CrudRepository is an interface in Spring Data JPA that provides basic CRUD (Create, Read, Update, Delete) operations for database entities. It is commonly used in Spring Boot applications to simplify interaction with the database without writing implementation code.
Syntax:
public interface CrudRepository<T, ID> extends Repository<T, ID>
Where:
Example:
public interface StudentRepository extends CrudRepository<Student, Long> { }
JpaRepository is a JPA (Java Persistence API) specific extension of Repository. It contains the full API of CrudRepository and PagingAndSortingRepository. So it contains API for basic CRUD operations and also API for pagination and sorting.
Syntax:
public interface JpaRepository<T,ID> extends PagingAndSortingRepository<T,ID>, QueryByExampleExecutor<T>
Where:
Example:
public interface StudentRepository extends CrudRepository<Student, Long> { }
The Spring Data Repository Interface provides a simple way to perform database operations in Spring Boot applications. It reduces the need to write boilerplate code by automatically implementing common data access methods.
| Feature | CrudRepository | JpaRepository |
|---|---|---|
| Definition | Provides basic CRUD operations for entities. | Extends CrudRepository and provides advanced JPA features. |
| Inheritance | Base repository interface. | Extends CrudRepository and PagingAndSortingRepository. |
| Functionality | Supports basic methods like save(), findById(), findAll(), delete(). | Includes all CRUD methods plus pagination and sorting features. |
| Additional Methods | Limited to basic CRUD operations. | Provides extra methods like flush(), saveAndFlush(), deleteInBatch(). |
| Use Case | Used for simple database operations. | Used for advanced database operations with JPA. |
| Performance Control | No direct control over persistence context. | Provides better control over JPA persistence and batch operations. |