1

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 Answers1

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

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • I am doing bulk operations with HQL not SQL. However, again in-memory state of objects in not affected. – Efstathios Chatzikyriakidis Nov 07 '14 at 15:06
  • HQL is what I am talking about here. NOT SQL. The DML operations are bout HQL and they cannot be connected with in memory session – Radim Köhler Nov 07 '14 at 15:57
  • Hmm. Is there any other way of executing bulk saves or updates without using HQL? In memory state is something I want to support... – Efstathios Chatzikyriakidis Nov 07 '14 at 20:16
  • Well, you should most likely read some parts from documentation, e.g. [Contextual session](http://nhforge.org/doc/nh/en/index.html#architecture-current-session) and also the DML mentioned in my answer. After some reading, you should understand, that there is either session management, with one by one insert/update statements related to changed/dirty entities. That could be batched *(kind of bulk)* on ADO.NET level. See [this answer](http://stackoverflow.com/a/26806383/1679310) and comments. But DML is different story. Not related to session. One or the other. Does it help now? – Radim Köhler Nov 09 '14 at 10:18
  • Yes it is a wonderful tool. I hope in the future the NHibernate people to create something like the following: http://blogs.telerik.com/openaccessteam/posts/13-06-24/bulk-update-and-delete-as-easy-as-linq – Efstathios Chatzikyriakidis Nov 09 '14 at 10:51