I would like to intercept persistence operations over collection properties, decide by myself if it can be synchronized with database, and call a procedure when a persistence is decided to occur over all collection at once and not by each element.
How to do that ?
These collection properties are usually mapped with one-to-many or many-to-many associations. So, when we have something like this:
myEntity.List.Add(new Item());
myEntity.List.Add(new Item());
...
session.Save(myEntity);
For a mapping having two classes (entities) and an unidirectional many-to-many association, I would like to have only two sql statements occurring: INSERT INTO, and a PROCEDURE CALL which expects to receive a list of values comma-separated that is the key values from the List collection above. The collection of keys can only be saved on this system calling a procedure with a list of values (csv).
This kind of customizing is possible to be done ?
Well, I adopted a solution implementing NHibernate Listeners, so I have a listener like that:
And my object
MyActionBeforeCommit()is scheduled to run only when and if a transaction is commited. So, in my “future action” I have:And finally we need to register the new listener on NHibernate configuration, like that:
And It works very well. This is a very nice NHibernate feature, very powerful.