'In Spring Boot what is the difference between CrudRepository and JpaRepository in extending a Java repository interface

when building respositories in spring, in my repository interface i extend it using the below

extends CrudRepository<EntityName, EntityType> where EntityName is the name of my Entity Class and EntityType is set to default type Long see sample code below

@Repository
public interface RoomRepository extends CrudRepository<Room, Long> {
}

I have noticed however the use of JpaRepository see example below

public interface RoomRepository extends JpaRepository<Room, UUID>{

    public Boolean existsRoom(String roomNumber);
    
}


Solution 1:[1]

CrudRepository vs JpaRepository

JpaRepository extends PagingAndSortingRepository which in turn extends CrudRepository.

  1. CrudRepository mainly provides CRUD functions.
  2. PagingAndSortingRepository provides methods to do pagination and sorting records.
  3. JpaRepository provides some JPA-related methods such as flushing the persistence context and deleting records in a batch.

Because of the inheritance mentioned above, JpaRepository will have all the functions of CrudRepository and PagingAndSortingRepository.

So if you don't need the repository to have the functions provided by JpaRepository and PagingAndSortingRepository, use CrudRepository.

Reference: https://www.javatpoint.com/spring-boot-crud-operations#:~:text=CrudRepository%20does%20not%20provide%20any,works%20as%20a%20marker%20interface.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chatla harishwar