'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]
JpaRepository extends PagingAndSortingRepository which in turn extends CrudRepository.
- CrudRepository mainly provides CRUD functions.
- PagingAndSortingRepository provides methods to do pagination and sorting records.
- 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.
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 |

