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

问题描述

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



我对以下内容感到困惑:


  1. JPA,JTA,Hibernate有自己的自己的交易方式
    管理。作为一个例子,考虑如果我使用Spring + Hibernate,在
    的情况下你会使用JPA事务吗?



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

  2. @Transactional 注释,是特定于Spring
    Framework吗?从我的理解,这个注释是Spring
    特定的框架。如果这是正确的,是 @Transactional 使用
    JPA / JTA来完成交易控制?


我在网上阅读以清除我的疑惑,但是我没有直接回答。任何输入都会有很大的帮助。

解决方案

@Transactional Spring-> Hibernate 使用 JPA ie



@Transactional 注解应该放在所有不可分割的操作中。

所以让我们举个例子:



我们有2个模型,即国家城市
国家城市模型的关系映射就像一个国家可以有多个城市一样,因此映射就像,

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

这里Country将Lazily映射到多个城市。
因此,当我们从数据库中检索Country对象时,这里是 @Transactinal 的角色,那么我们将获取Country对象的所有数据,但不会获得Set of cities,因为我们正在取得城市LAZILY。

  //没有@Transactional 
public Country getCountry(){
Country country = countryRepository.getCountry( );
//在CountryRepository和数据库之间获得Country对象关联后关闭
}

当我们想从国家对象访问Set of Cities时,我们将在该Set中获得空值,因为Set的对象只创建了这个Set,并没有初始化那里的数据来获取Set的值,我们使用 @Transactional 即,

  //带有@Transactional 
@Transactional
公共国家getCountry(){
Country country = countryRepository.getCountry();
//当我们初始化使用对象国家的城市,以便与数据库直接通信并从数据库中检索所有城市时,这仅仅是因为@Transactinal
Object object = country.getCities()。size();

$ / code>

所以基本上 @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. Do JPA , JTA, Hibernate have their "own" way of transactionmanagement. As an example, consider if I use Spring + Hibernate, inthat case would u use "JPA" transactions?

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

  2. The @Transactional annotation, is that specific to SpringFramework? From what I understood, this annotation is SpringFramework specific. If this is correct, is @Transactional usingJPA/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 of Spring->Hibernate using JPA i.e.

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

So lets take example:

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;

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
}

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();
}

So basically @Transactional is Service can make multiple call in single transaction without closing connection with end point.

Hope this will helpful to you.

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

08-04 07:09
查看更多