如何在Spring Data中使用@Transactional? [英] How to use @Transactional with Spring Data?

查看:99
本文介绍了如何在Spring Data中使用@Transactional?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始研究Spring-data,Hibernate,MySQL,JPA项目。我切换到spring-data,这样我就不用担心手工创建查询了。



我注意到使用 @Transactional因为我在没有注释的情况下尝试了我的查询,所以当你使用spring-data时不需要



是否有特定原因我应该/不应该使用 @Repository 注释?



Works:

  @Transactional 
public List listStudentsBySchool(long id){
return repository.findByClasses_School_Id(id);
}

同样有效:

  public List listStudentsBySchool(long id){
return repository.findByClasses_School_Id(id);
}

提前致谢!

解决方案

你的问题实际上是什么?使用 @Repository 注释或 @Transactional



<完全不需要p> @Repository ,因为您声明的接口将由Spring Data Infrastructure创建并激活异常转换的代理支持。因此,在Spring Data存储库接口上使用此注释根本没有任何效果。



@Transactional - 对于JPA模块我们在支持代理的实现类上有这个注释( SimpleJpaRepository )。这有两个原因:首先,持久化和删除对象需要JPA中的事务。因此,我们需要确保一个事务正在运行,我们通过使用 @Transactional 注释该方法来完成。



读取方法如 findAll() findOne(...)正在使用 @Transactional(readOnly = true)这不是绝对必要的,但会在事务基础结构中触发一些优化(设置 FlushMode MANUAL 让持久性提供程序在关闭 EntityManager 时可能会跳过脏检查。除此之外,该标志也在JDBC连接上设置,这导致该级别的进一步优化。



根据您使用的数据库,它可以省略表锁,甚至拒绝您可能意外触发的写操作。因此,对于查询方法,我们建议使用 @Transactional(readOnly = true),您可以轻松地将该注释添加到存储库接口。确保在您可能已在该界面中声明或重新修饰的操作方法中添加一个简单的 @Transactional


I just started working on a Spring-data, Hibernate, MySQL, JPA project. I switched to spring-data so that I wouldn't have to worry about creating queries by hand.

I noticed that the use of @Transactional isn't required when you're using spring-data since I also tried my queries without the annotation.

Is there a specific reason why I should/shouldn't be using the @Repository annotation?

Works:

@Transactional
public List listStudentsBySchool(long id) {
    return repository.findByClasses_School_Id(id);
}

Also works:

public List listStudentsBySchool(long id) {
    return repository.findByClasses_School_Id(id);
}

Thanks in advance!

解决方案

What is your question actually about? The usage of the @Repository annotation or @Transactional.

@Repository is not needed at all as the interface you declare will be backed by a proxy the Spring Data infrastructure creates and activates exception translation for anyway. So using this annotation on a Spring Data repository interface does not have any effect at all.

@Transactional - for the JPA module we have this annotation on the implementation class backing the proxy (SimpleJpaRepository). This is for two reasons: first, persisting and deleting objects requires a transaction in JPA. Thus we need to make sure a transaction is running, which we do by having the method annotated with @Transactional.

Reading methods like findAll() and findOne(…) are using @Transactional(readOnly = true) which is not strictly necessary but triggers a few optimizations in the transaction infrastructure (setting the FlushMode to MANUAL to let persistence providers potentially skip dirty checks when closing the EntityManager). Beyond that the flag is set on the JDBC Connection as well which causes further optimizations on that level.

Depending on what database you use it can omit table locks or even reject write operations you might trigger accidentally. Thus we recommend using @Transactional(readOnly = true) for query methods as well which you can easily achieve adding that annotation to you repository interface. Make sure you add a plain @Transactional to the manipulating methods you might have declared or re-decorated in that interface.

这篇关于如何在Spring Data中使用@Transactional?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆