Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

spring

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);
    
}
like image 277
iamdeed Avatar asked Nov 14 '25 09:11

iamdeed


1 Answers

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.

like image 94
chatla harishwar Avatar answered Nov 17 '25 08:11

chatla harishwar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!