Do I have to refresh entities after bulk updates /

2020-04-17 07:56发布

I have written some DAO methods that do bulk updates / deletes with HQL but I see that when the query is executed the entities in memory are not sychronized (the cache is not updated).

Say, I have a collection of Projects with a collection of Groups each and I want to delete all Groups. I can iterate the Groups and delete each but I prefer to run a bulk delete with HQL and IN operator. However, the list has the old objects after the query is executed.

I realized that I have to refresh the objects with session.refresh(). Is there any other way I can bulk update and update cache automatically?

1条回答
走好不送
2楼-- · 2020-04-17 08:38

The answer is NO. As documented here:

13.3. DML-style operations

cite:

...As already discussed, automatic and transparent object/relational mapping is concerned with the management of object state. This implies that the object state is available in memory, hence manipulating (using the SQL Data Manipulation Language (DML) statements: INSERT, UPDATE, DELETE) data directly in the database will not affect in-memory state. However, NHibernate provides methods for bulk SQL-style DML statement execution which are performed through the Hibernate Query Language (HQL)...

DML is there for us, to skip the state management in memory...being more efficient from SQL statements point of view. But that means:

manipulating ... data directly in the database will not affect in-memory state

查看更多
登录 后发表回答