JPA/JTA/@Transactional Spring 注解 [英] JPA / JTA / @Transactional Spring annotation

查看:26
本文介绍了JPA/JTA/@Transactional Spring 注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读使用 Spring 框架的事务管理.在第一个组合中,我使用 Spring + hiberante 并使用 Hibernate 的 API 来控制事务(Hibernate API).接下来,我想使用 @Transactional 注释进行测试,它确实有效.

I am reading the transaction management Using Spring framework. In first combination I used Spring + hiberante and used Hibernate's API's to control the transaction (Hibenate API's). Next, I wanted to test using @Transactional annotation, and it did work.

我对以下内容感到困惑:

I am getting confused on:

  1. JPA、JTA、Hibernate 是否有自己"的交易方式管理.例如,考虑如果我使用 Spring + Hibernate,在在这种情况下,您会使用JPA"交易吗?

  1. Do JPA , JTA, Hibernate have their "own" way of transaction management. As an example, consider if I use Spring + Hibernate, in that case would u use "JPA" transactions?

就像我们有 JTA 一样,说我们可以使用 Spring 和 JTA 是真的吗?控制交易?

Like we have JTA, is it true to say we can use Spring and JTA to control transactions?

@Transactional 注释,是 Spring 特有的框架?据我了解,这个注解是Spring特定于框架.如果这是正确的,是 @Transactional 使用JPA/JTA 来做事务控制?

The @Transactional annotation, is that specific to Spring Framework? From what I understood, this annotation is Spring Framework specific. If this is correct, is @Transactional using JPA/JTA to do the transaction control?

我确实在网上阅读以消除我的疑虑,但是我没有得到直接的答案.任何输入都会有很大帮助.

I do read online to clear my doubts, however something I don't get direct answer. Any inputs would be great help.

推荐答案

@Transactional in case Spring->Hibernate using JPA

@Transactional in case of Spring->Hibernate using JPA i.e.

@Transactional 注释应该放在所有不可分割的操作周围.

@Transactional Annotations should be placed around all operations that are inseparable.

让我们举个例子:

我们有 2 个模型,即 CountryCity.CountryCity 模型的关系映射就像一个 Country 可以有多个 Cities 所以映射就像,

We have 2 model's i.e. Country and City. Relational Mapping of Country and City model is like one Country can have multiple Cities so mapping is like,

@OneToMany(fetch = FetchType.LAZY, mappedBy="country")
private Set<City> cities;

这里 Country 映射到多个城市,并且懒惰地获取它们.因此,当我们从数据库中检索 Country 对象时,@Transactinal 的作用就出现了,然后我们将获得 Country 对象的所有数据,但不会获得城市集,因为我们正在懒惰地获取城市.

Here Country mapped to multiple cities with fetching them Lazily. So here comes role of @Transactinal when we retrieve Country object from database then we will get all the data of Country object but will not get Set of cities because we are fetching cities LAZILY.

//Without @Transactional
public Country getCountry(){
   Country country = countryRepository.getCountry();
   //After getting Country Object connection between countryRepository and database is Closed 
}

当我们想从 country 对象访问 Set of Cities 时,我们将在该 Set 中获得空值,因为仅创建此 Set 的 Set 对象未使用那里的数据进行初始化以获取 Set 的值,我们使用 @Transactional 即,

When we want to access Set of Cities from country object then we will get null values in that Set because object of Set created only this Set is not initialize with there data to get values of Set we use @Transactional i.e.,

//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服务可以在单个事务中进行多次调用而无需关闭与端点的连接.

希望对您有所帮助.

这篇关于JPA/JTA/@Transactional Spring 注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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