FlushMode.AUTO后面检查什么?
在Hibernate中,我想知道当flushMode为AUTO时哪些条件会触发刷新?它可能很复杂(或“神奇”),但基本条件是什么?
谢谢
In Hibernate, I wonder which conditions trigger flushing when flushMode is AUTO? It may be complex (or "magic") but what are the basic conditions?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当刷新模式为
FlushMode时。 AUTO 它将在以下时间发生:
此模式的目的是避免“陈旧”状态。对内存中对象所做的更改可能与查询结果发生冲突。我的理解是 Hibernate 会进行“脏检查”,比较对象的原始状态和当前状态。如果它们不同,并且 Hibernate 认为这种差异会让您接触到过时的数据,它会尝试将此状态推送到数据库。这是可能的,因为 Hibernate 知道查询将“触及”哪些表以及需要针对当前脏状态更新哪些表。
看看这篇文章:
您还可以查看 org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush 源代码。
FlushMode.AUTO 有几个缺点:
由于这些缺点,我更喜欢
FlushMode.COMMIT
,它可以为您提供更多控制。在这种模式下,Hibernate 会等待,直到您显式调用 Commit 或 Flush,然后才将内存中的状态与数据库同步。When flush mode is
FlushMode.AUTO
it will happen at following times:The purpose of this mode is to avoid 'stale' state. Changes made to objects in memory may conflict with the results of the query. My understanding is that Hibernate will do a 'dirty-checking', compare original and current state of objects. If they are different and Hibernate thinks that this difference will expose you to stale data, it will try to push this state to database. It is possible because Hibernate knows what tables will be 'touched' by query and what tables it will need to update for current dirty state.
Take a look at this article:
You can also look at
org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush
source code.There are couple disadvantages of
FlushMode.AUTO
:Because of these disadvantages I prefer
FlushMode.COMMIT
which gives you more control. In this mode Hibernate waits until you explicitly call Commit or Flush and only synchronizes in-memory state with database after that.