在Spring中,我们是否需要@Transactional和@Modifying批注? [英] Do we need both @Transactional and @Modifying annotation in Spring?
问题描述
我仍在努力思考@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.
修改查询
- 修改方法签名只能返回
void
,Integer
或int
- 更新查询必须是事务性的,并标记为
@Transactional
- Spring Data将删除所有未刷新的未完成更改,这些更改将在
EntityManager
中进行更改,并使用@Modifying(clearAutomatically=false)
- Modifying method signature can only return
void
,Integer
orint
- Updating queries MUST be transactional, mark with
@Transactional
- 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();
}
我的问题是关于:
- 为什么在存储库
@Repository
级别上有服务类时为什么需要在服务类中有@Transactional
并且我在方法上有@Modifying
哪个会修改实体并将其写入数据库(仅因为我在类级别具有@Transactional(readOnly = true)
)? - Service类上的注释
@Transactional
是否推广到@Repository
类?
- 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) ? - 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屋!