使用HQL批量更新/删除后,我是否必须刷新实体? [英] Do I have to refresh entities after bulk updates / deletes with HQL?
问题描述
我已经编写了一些使用HQL进行批量更新/删除的DAO方法,但是我发现执行查询时内存中的实体未同步(缓存未更新).
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).
说,我有一个项目集合,每个项目都有一个组,我想删除所有组.我可以迭代组并删除每个组,但是我更喜欢使用HQL和IN运算符运行批量删除.但是,执行查询后,列表中有旧对象.
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.
我意识到我必须使用session.refresh()刷新对象.我还有其他方法可以批量更新并自动更新缓存吗?
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?
推荐答案
答案为否.如此处所述:
The answer is NO. As documented here:
引用:
...如上所述,自动透明的对象/关系映射关系到对象状态的管理. 这意味着对象状态在内存中可用,因此进行操作(使用SQL数据操作语言(DML)语句:INSERT,UPDATE,DELETE) em> 直接在数据库中的数据不会影响内存中状态.但是,NHibernate提供了用于执行SQL样式的DML语句的方法,这些方法是通过Hibernate查询语言(HQL)执行的.
...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为我们服务,可以跳过内存中的状态管理...从SQL语句的角度来看更加高效.但这意味着:
DML is there for us, to skip the state management in memory...being more efficient from SQL statements point of view. But that means:
直接在数据库中操作...数据不会影响内存中状态
这篇关于使用HQL批量更新/删除后,我是否必须刷新实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!