栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JPA / JTA / @Transactional Spring批注

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JPA / JTA / @Transactional Spring批注

@Transactional
如果
Spring->Hibernate
使用
JPA
ie

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

让我们举个例子:

我们有2个模型的ie

Country
City
Country
City
模型的关系映射就像一个国家可以有多个城市,因此映射就像,

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

在这里,国家/地区映射到多个城市,并懒洋洋地获取它们。因此,

@Transactinal
当我们从数据库中检索Country对象时,我们将获得Country对象的所有数据,但由于获得LAZILY而无法获取城市集,因此将发挥作用。

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

当我们要从国家对象访问城市集时,我们将在该集合中获得空值,因为仅创建了该集合的Set的对象未使用那里的数据初始化来获取我们使用的Set的值,

@Transactional
即,

//with @Transactional@Transactionalpublic 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
,Service可以在单个事务中进行多个调用,而无需关闭与端点的连接。

希望这对你有所帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402495.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号