在Spring中,我们是否需要@Transactional和@Modifying批注? [英] Do we need both @Transactional and @Modifying annotation in Spring?

查看:187
本文介绍了在Spring中,我们是否需要@Transactional和@Modifying批注?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在努力思考@Transactional的工作原理.

I am still trying to wrap my head around how @Transactional works.

我在Service类的方法上具有@Transactional批注,在Repository类中的方法上具有@Modifying批注.带有@Transactional批注的方法是否适用于存储库中带有@Modifying批注的方法?

I have @Transactional annotation on Service class's method and @Modifying annotation on the method in the Repository class. Does method with @Transactional annotation applies to the method in the Repository with annotation @Modifying?

我的理解:

@Transactional在具有@Transactional( readOnly = true )的类的方法上,没有数据被写入数据库,而对于@Transactional,则数据被写入数据库.

@Transactional on a method of a class with @Transactional( readOnly = true ) no data gets written to the database, whereas with @Transactional, the data gets written to the database.

修改查询

  1. 修改方法签名只能返回voidIntegerint
  2. 更新查询必须是事务性的,并标记为@Transactional
  3. Spring Data将删除所有未刷新的未完成更改,这些更改将在EntityManager中进行更改,并使用@Modifying(clearAutomatically=false)
  1. Modifying method signature can only return void, Integer or int
  2. Updating queries MUST be transactional, mark with @Transactional
  3. Spring Data will drop all non-flushed changes pending in the EntityManager, change with @Modifying(clearAutomatically=false)

第二点说@Modifying查询必须具有@Transactional(readOnly=false),因此我们可以在@Service级方法调用或@Repository方法级调用中添加它.如果在@Service级别添加,它是否也适用于@Respository方法,该方法是从@Service级别方法调用中调用的?

As the second point says @Modifying queries must have @Transactional(readOnly=false), so we can either add it at @Service level method call or @Repository method level call. If added at the @Service level it applies to the @Respository method too which is being called from the @Service level method call?

示例:

@Service
class AnimalServiceImpl implements AnimalService {

@Autowire
AnimalRepository animalRepository;

@Override
@Transactional
public void persistAnimal() {
....
animalRepository.save();
}

@Override
@Transactional(readOnly = true)
public void checkIfAnimalPresent() {

...
animalRepository.checkIfPresent();

}

@Override
@Transactional
public void deleteAnimal() {
...
animalRepository.deleteAnimal();
}
}

存储库

@Repository
@Transactional(readOnly=true)
public interface AnimalRepository extends org.springframework.data.repository.Repository {

@Modifying
@Query(...)
void save();

@Modifying
@Query(...)
int checkIfPresent() 

@Modifing
@Query(..)
int deleteAnimal();
}

我的问题是关于:

  1. 为什么在存储库@Repository级别上有服务类时为什么需要在服务类中有@Transactional并且我在方法上有@Modifying 哪个会修改实体并将其写入数据库(仅因为我在类级别具有@Transactional(readOnly = true))?
  2. Service类上的注释@Transactional是否推广到@Repository类?
  1. Why do we need @Transactional in the service class when we have it at the repository @Repository level and I have @Modifying on methods which modify the entity and writes it to the database (only because I have @Transactional(readOnly = true) at the class level) ?
  2. Does the annotation @Transactional on the Service class propogate to @Repository class?

我希望我对这里的问题和例子很清楚.

I hope I am very clear with my questions and examples here.

推荐答案

@Transactional的工作方式:

默认情况下,存储库实例上的CRUD方法是事务性的.
对于读取操作,事务配置readOnly标志设置为true.
所有其他文件都配置有普通的@Transactional,以便应用默认事务配置.
如果需要调整存储库中声明的方法之一的事务配置,则可以覆盖该方法,并添加具有必需属性值的@Transactional批注.

By default, CRUD methods on repository instances are transactional.
For read operations, the transaction configuration readOnly flag is set to true.
All others are configured with a plain @Transactional so that default transaction configuration applies.
If you need to tweak transaction configuration for one of the methods declared in a repository you can override that method and add @Transactional annotation with required attribute values.

另一种改变交易行为的方法是使用外观或服务实现(通常)覆盖多个存储库.
其目的是为非CRUD操作定义事务边界.

Another way to alter transactional behavior is to use a facade or service implementation that (typically) covers more than one repository.
Its purpose is to define transactional boundaries for non-CRUD operations.

如果使用这种方法,则存储库中的事务配置将被忽略,因为外部事务(在服务层中定义)配置确定了实际使用的事务.

If you use this approach, the transaction configuration at the repositories is then neglected, as the outer transaction (defined in the service layer) configuration determines the actual one used.

参考: Spring Data JPA-参考文档-5.7.交易性

这篇关于在Spring中,我们是否需要@Transactional和@Modifying批注?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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