清理会话,刷新后,休眠批量更新? [英] Clearing session, flushing, refreshing, after hibernate bulk updates?

查看:103
本文介绍了清理会话,刷新后,休眠批量更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我们所知道的那样,当用hibernate(甚至在HQL中)对数据库进行批量更新时,所做的更改不会复制到存储在当前会话中的实体。



我们经常调用flush来将修改发送给数据库,但文档中提到它同步会话和数据库...

这是否意味着flush可以将新的db值设置为我的会话实体?或刷新最终将擦除我的新数据库值与存储在实体中的旧数据值?
(如果hibernate的行为是第一个行为,那么它是如何检测哪个是好值的?)。

如果我不能使用在这种情况下刷新,在每次批量更新之后清除会话是一个很好的做法,这样我们在会话中肯定会有很好的价值? 解决方案

所有 flush 都会将以前缓存的SQL语句发送到数据库。它不会改变已经在会话中的对象。在某种程度上,它与你所需要的相反。刷新的SQL语句可能会覆盖您的批量更新更改。在更新之前,你可能想要做的是 flush(),然后是 clear()。或者,如果您不想清除整个缓存, evict()。我从来没有试过 refresh(),但它似乎也可以工作。


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?

解决方案

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.

这篇关于清理会话,刷新后,休眠批量更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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