Difference between CRUDRepository and JPARepository.

CrudRepository and JpaRepository are both Spring Data repository interfaces, but JpaRepository extends CrudRepository (via PagingAndSortingRepository) to add JPA-specific and pagination-related capabilities on top of the basic CRUD operations.

Key Points: • CrudRepository provides the fundamentals: save(), findById(), findAll(), deleteById(), count(), and existsById(). • JpaRepository inherits everything from CrudRepository and PagingAndSortingRepository, adding methods like flush(), saveAndFlush(), deleteInBatch(), and findAll(Pageable) / findAll(Sort). • Because JpaRepository extends CrudRepository, you never lose any CRUD functionality by choosing it. • In practice, most Spring Boot projects default to JpaRepository since pagination, sorting, and batch operations are commonly needed. • CrudRepository can be a good, more minimal choice for very simple repositories or non-JPA Spring Data modules (like MongoDB) where JPA-specific methods don't apply.

Example: A UserRepository that only ever needs save() and findById() could extend CrudRepository, but the moment the application needs a paged listing of users sorted by name, it needs to extend JpaRepository instead to get access to findAll(Pageable).

Code Example:

public interface UserRepository extends JpaRepository<User, Long> {
    // inherits save(), findById(), findAll(Pageable), flush(), etc.
}

Interview Tip: A concise interview answer is:

"JpaRepository extends CrudRepository, so it includes all the basic CRUD methods plus JPA-specific extras like flushing changes, batch deletes, and pagination/sorting support via findAll(Pageable). I default to JpaRepository in Spring Boot projects since I rarely know upfront that I'll never need pagination or batching."