休眠保存方法-事务外 [英] hibernate save method - outside of transaction

查看:96
本文介绍了休眠保存方法-事务外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从下面的链接:

save()方法不能保证相同,它返回一个标识符,并且如果必须执行INSERT来获取标识符(例如"identity"生成器),则无论您位于内部还是内部,此INSERT都会立即发生.在交易之外.

save() method does not guarantee the same, it returns an identifier, and if an INSERT has to be executed to get the identifier (e.g. "identity" generator), this INSERT happens immediately, no matter if you are inside or outside of a transaction.

http://javarevisiteded .blogspot.com/2012/09/difference-hibernate-save-vs-persist-and-saveOrUpdate.html

我有一个疑问:即使方法不在事务内,如何保存方法也能立即插入?因为如果发生插入这意味着它正在命中数据库,而数据库操作怎么可能没有任何事务呢? AFAIK事务的最小粒度是单个插入/更新/选择/删除语句.所以我需要澄清.

I have this doubt: how can save method insert immediately even if it is outside of transaction? Because if insert happens that means it is hitting the database and how can be a database operation be without any transaction? AFAIK the smallest granularity of a transaction is a single insert/update/select/delete statement. So I need clarification.

推荐答案

从数据库中检索实体后,该实体将附加到会话中,并仍然是会话的一部分. 会话,直到会话(及其事务)结束.同样地, 当添加或更新实体时,这些更改也将附加到会话中,并且当 实体从数据库中删除,从会话中删除.

When an entity is retrieved from the database, it is attached to the Session and remains part of the Session until the Session (and its transaction) ends. Likewise, when an entity is added or updated, those changes are also attached to the Session, and when an entity is deleted from the database, it is removed from the Session.

但是,您的陈述(或您提供的任何链接)是正确的.

However, that statement of yours (or whatever that link you provided) is true.

休眠save() 可用于将实体保存到数据库.我们可以在事务外部调用此方法.如果我们不使用事务就使用它,并且在实体之间进行级联,那么除非刷新会话,否则只有主实体会被保存.

Hibernate save() can be used to save entity to database. We can invoke this method outside a transaction. If we use this without transaction and we have cascading between entities, then only the primary entity gets saved unless we flush the session.

您可以找到一个有效的示例这里:

You can find a working example here:

这篇关于休眠保存方法-事务外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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