java 在休眠批量更新后清除会话、刷新、刷新?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7757244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clearing session, flushing, refreshing, after hibernate bulk updates?
提问by Sebastien Lorber
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.
正如我们所知,当使用 hibernate(即使在 HQL 中)对 DB 进行批量更新时,所做的更改不会复制到当前会话中存储的实体。
So i may call session.refresh to load the modifications to my session entities.
所以我可以调用 session.refresh 将修改加载到我的会话实体中。
We often call flush for sending our modifications to the DB, but the documentation say it "synchronize" the session and the db...
我们经常调用flush来将我们的修改发送到数据库,但文档说它“同步”了会话和数据库......
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"?).
这是否意味着刷新将能够为我的会话实体设置好的新 db 值?还是刷新最终会用存储在实体中的旧值擦除我的新数据库值?(顺便说一句,如果休眠的行为是第一个,它如何检测哪个是“好的价值”?)。
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?
如果我不能在这种情况下使用刷新,那么在每次批量更新后清除会话是一个很好的做法,以便我们确保在我们的会话中具有良好的值?
采纳答案by Alex Gitelman
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 is flush()
and then clear()
before your update. Or, if you don't want to clear the entire cache, evict()
. I never tried refresh()
but it seems that it will also work.
所flush
要做的就是将先前缓存的 SQL 语句发送到数据库。它不会更改您已经在会话中的对象。在某种程度上,它与您的需要相反。来自刷新的 SQL 语句可能会覆盖您的批量更新更改。您可能想要做的是flush()
然后clear()
在更新之前。或者,如果您不想清除整个缓存,evict()
. 我从未尝试过,refresh()
但似乎它也可以工作。