英文:
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<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?
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论