Clearing session, flushing, refreshing, after hibe

2020-07-07 11:43发布

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?

1条回答
神经病院院长
2楼-- · 2020-07-07 12:10

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.

查看更多
登录 后发表回答