Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is the difference between JpaRepository and CrudRepository using Spring Data JPA? [duplicate]

I am working on a Spring Boot application that uses Spring Data JPA (on Hibernate 4) to access to my DB.

My doubt is related on the DAO interfaces (used by JPA to automatically generate the queries).

So, in my project I have these 2 interfaces:

1) AccomodationDAO:

@Repository
@Transactional(propagation = Propagation.MANDATORY)
public interface AccomodationDAO extends JpaRepository<Accomodation, Long> {

    Accomodation findById(@Param("id") Long id);

}

2) EventDAO:

public interface EventDAO extends CrudRepository<Event, Integer> {

    public Event findByLocation(Point location);

    public Event findById(@Param("id") Integer id);

}

They both works fine and use the same logic to declare queryes.

My only doubt is: the first one extends JpaRepository while the second one implements CrudRepository.

What exactly is the difference between JpaRepository and CrudRepository? What is the best choise to use or in what case is better use one instead the other choice?

Another doubt is: why my defined DAO interfaces extends JpaRepository and CrudRepository that are themselves interfaces? From what I know the interfaces are implemented and not extended...what am I missing?

like image 240
AndreaNobili Avatar asked Nov 02 '16 14:11

AndreaNobili


People also ask

What is difference between CrudRepository and JpaRepository interface in Spring data JPA?

Each of these defines its own functionality: CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.

What is the difference between CrudRepository and JpaRepository Linkedin?

Here in the following image Repository, CrudRepository and PagingAndSortingRepository belong to Spring Data Commons whereas JpaRepository belongs to Spring Data JPA. It is a base interface and extends Repository Interface. It extends PagingAndSortingRepository that extends CrudRepository.

Which is better CrudRepository or JpaRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

What is the difference between credit repository and JpaRepository?

Their main functions are: 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.


1 Answers

Note that JpaRepository extends CrudRepository. Compare the JavaDoc of these two interfaces:

JpaRepository vs CrudRepository

In short JpaRepository

  • has additional JPA specific methods that support for example Query By Example , deleting in batches, manual flushing changes to database
  • querying methods return List's instead of Iterable's

If you are using JPA you should use JpaRepository.

like image 185
Robert Niestroj Avatar answered Oct 24 '22 09:10

Robert Niestroj