PagingAndSortingRepository在运行时将使用哪种实现?

huangapple go评论75阅读模式
英文:

What implementation will be used for PagingAndSortingRepository in runtime?

问题

我对Spring Data JPA有点困惑。能有人解释一下以下内容吗?

假设我有这个接口:

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

在运行时会使用PagingAndSortingRepository的哪个实现?

我需要在使用这个repository的类或方法上添加@Transactional注解吗?

英文:

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&lt;User, Long&gt; {
}

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?

答案1

得分: 2

以下是翻译好的部分:

实现PagingAndSortingRepository中声明的方法是在SimpleJpaRepository中定义的。

如果您在事务内只需要通过单个存储库调用来完成所有操作,您不需要额外的@Transactional,但通常您希望事务涵盖多个调用或至少涵盖加载操作和随后的实体操作。在这些情况下,您通常会使用带有@Transactional注释的方法。但是您也可以使用例如TransactionTemplate,这在某些类型的测试中可能会很有用。

英文:

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.

huangapple
  • 本文由 发表于 2020年10月22日 18:14:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64480112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定