What implementation will be used for PagingAndSortingRepository in runtime?

Issue

I’m a bit confused with Spring Data JPA. Could anybody explain the following?

Let’s imagine I have the interface

@Repository
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
}

What exactly the implementation of PagingAndSortingRepository will be used in runtime?

And I need to put @Transactional annotation on class or methods that will be using this repository?

Solution

The implementation for the methods declared in the PagingAndSortingRepository are defined in SimpleJpaRepository.

If all what you want to do within a transaction is handled by a single repository call you don’t need any extra @Transactional, but typically you do want the transaction to cover more then one call or at least a load operation and the manipulation of an entity afterwards. In those cases you would typically use a method annotated with @Transactional. But you could also use for example a TranactionTemplate which can be nice for example in certain kind of tests.

Answered By – Jens Schauder

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published