休眠批量更新后清除会话、刷新、刷新?
众所周知,当使用 hibernate 对数据库进行批量更新时(即使在 HQL 中),所做的更改不会复制到当前会话中存储的实体。
所以我可以调用 session.refresh 来加载对我的会话实体的修改。
我们经常调用flush来将我们的修改发送到数据库,但文档说它“同步”会话和数据库...
这是否意味着flush将能够为我的会话实体设置良好的新数据库值?或者刷新最终会删除我的新数据库值与存储在实体中的旧数据库值? (顺便说一句,如果 hibernate 的行为是第一个,它如何检测哪一个是“好的价值”?)。
如果我不能在这种情况下使用刷新,那么在每次批量更新后清除会话是一个很好的做法,这样我们就可以确保会话中具有良好的值?
As we know, when doing a bulk update to the DB with hibernate (even in HQL), the changes made are not replicated to the entities stored in the current session.
So i may call session.refresh to load the modifications to my session entities.
We often call flush for sending our modifications to the DB, but the documentation say it "synchronize" the session and the db...
Does that mean that flush will be able to set the good new db value to my session entity? Or flush will eventually erase my new db value with the old one stored in the entity?
(Btw if hibernate's behaviour is the 1st one, how does it detect which one is the "good value"?).
If i can't use flush on such a case, it is a good practice to clear the session after each bulk update so that we are sure to have good values in our session?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有
flush
所做的就是将之前缓存的SQL 语句发送到数据库。它不会更改已经在会话中的对象。在某种程度上,它与你所需要的相反。来自刷新的 SQL 语句可能会覆盖您的批量更新更改。您可能想要执行的是flush()
,然后在更新之前执行clear()
。或者,如果您不想清除整个缓存,请使用evict()
。我从未尝试过refresh()
但它似乎也可以工作。All
flush
will do is sending previously cached SQL statements to the database. It will not change your objects that are already in session. In a way it does opposite to what you need. SQL statements from flush may, potentially, override your bulk update changes. What you probably want to do isflush()
and thenclear()
before your update. Or, if you don't want to clear the entire cache,evict()
. I never triedrefresh()
but it seems that it will also work.