Do I have to refresh entities after bulk updates / deletes with HQL?

自闭症网瘾萝莉.ら 提交于 2019-12-20 04:54:03

问题


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:


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



来源:https://stackoverflow.com/questions/26788120/do-i-have-to-refresh-entities-after-bulk-updates-deletes-with-hql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!