JPA/JTA/@Transactional-Spring注释
我正在阅读使用Spring框架的事务管理。在第一个组合中,我使用Spring+Hibernate并使用Hibernate的API来控制事务(HibernateAPI)。接下来,我想使用JPA/JTA/@Transactional-Spring注释,spring,hibernate,spring-mvc,jpa,annotations,Spring,Hibernate,Spring Mvc,Jpa,Annotations,我正在阅读使用Spring框架的事务管理。在第一个组合中,我使用Spring+Hibernate并使用Hibernate的API来控制事务(HibernateAPI)。接下来,我想使用@Transactional注释进行测试,它确实起了作用 我感到困惑的是: JPA、JTA、Hibernate是否有自己的交易方式 管理层。作为一个例子,考虑如果我使用Spring + Hibernate,在 在这种情况下,你会使用“JPA”交易吗 就像我们有JTA一样,我们可以使用Spring和JTA来 控制交易
@Transactional
注释进行测试,它确实起了作用
我感到困惑的是:
@Transactional
注释是专门针对Spring的
框架据我所知,这个注释就是Spring
具体框架。如果这是正确的,@Transactional
是否使用
JPA/JTA进行交易控制我确实在网上阅读来澄清我的疑问,但是有些事情我没有得到直接的答案。任何输入都会有很大帮助。
如果使用Spring->Hibernate
使用JPA
即
@Transactional
注释应该放在所有不可分割的操作周围
让我们举个例子:
我们有两种型号,即国家
和城市
。
国家
和城市
模型的关系映射就像一个国家可以有多个城市,所以映射就像
@OneToMany(fetch = FetchType.LAZY, mappedBy="country")
private Set<City> cities;
当我们希望从country对象访问城市集时,我们将在该集中获取空值,因为仅创建该集的集合对象未使用这些数据初始化,以获取我们使用的集合值@Transactional
,即
//with @Transactional
@Transactional
public Country getCountry(){
Country country = countryRepository.getCountry();
//below when we initialize cities using object country so that directly communicate with database and retrieve all cities from database this happens just because of @Transactinal
Object object = country.getCities().size();
}
所以基本上,@Transactional
是服务可以在单个事务中进行多次调用,而无需关闭与端点的连接
希望这会对您有所帮助。这个
@Transactional
注释分为两个包:javax.transaction
和org.springframework.transaction.annotation.Transactional
,所以我想应该有JTA/JPA事务处理和Spring事务处理,或者Spring实现JPA/JTA事务处理谢谢你的信息。幕后是“org.springframework.transaction.annotation.Transactional”Spring自己的交易机制或使用JPA或JTA的方式?您的上下文配置是什么?您使用什么事务管理器?请参考我前面问的这个问题,它有所有的代码/conf文件:是强制的吗?@LaHai是的,获取数据
//with @Transactional
@Transactional
public Country getCountry(){
Country country = countryRepository.getCountry();
//below when we initialize cities using object country so that directly communicate with database and retrieve all cities from database this happens just because of @Transactinal
Object object = country.getCities().size();
}