NHibernate for saving / updating event listeners: listening for child objects

I have an Area object that has many SubArea children:

public class Area { ... public virtual IList<SubArea> SubAreas { get; set; } } 

his children are displayed as unidirectional non-negative relationships:

 public class AreaMapping : ClassMap<Area> { public AreaMapping() { HasMany(x => x. SubAreas).Not.Inverse().Cascade.AllDeleteOrphan(); } } 

The area is my common root. When I save a region (e.g. Session.Save (region)), the region is saved and the child SubAreas is automatically cascaded.

I want to add a save or update event listener to catch when my areas and / or are saved. Say, for example, I have an area that has 5 SubAreas. If I connect to SaveEventListeners:

 Configuration.EventListeners.SaveEventListeners = new ISaveOrUpdateEventListener[] { mylistener }; 

When I save the region, Mylistener only starts once for the region (SubAreas are ignored). I want 5 SubAreas to be caught in the event listener as well. If I connect SaveOrUpdateEventListeners instead:

 Configuration.EventListeners.SaveOrUpdateEventListeners = new ISaveOrUpdateEventListener[] { mylistener }; 

When I save the area, the Mylistener is not at all . Oddly enough, if I connect to SaveEventListeners and SaveOrUpdateEventListeners:

 Configuration.EventListeners.SaveEventListeners = new ISaveOrUpdateEventListener[] { mylistener }; Configuration.EventListeners.SaveOrUpdateEventListeners = new ISaveOrUpdateEventListener[] { mylistener }; 

When I save the area, Mylistener starts 11 times : once for the area and twice for each SubArea! (I think because NHIbernate INSERTS SATURDAY, and then UPDATING with the foreign key of the field).

Does anyone know what I'm doing wrong here, and how can I get the listener to shoot once for each area and subarea?

+7
event-handling events nhibernate event-listener children
source share
2 answers

Not 100% related to your question, but if you match with inverse = "true" in your collection, at least you don't get insert and update instructions.

0
source share

NH issues an INSERT statement to know the identifier of an object if it cannot be known (for example, IDENTITY or SEQUENCE).

So, if you want to invalidate, you need to use an id generator that does not require roundtript for DB (e.g. guid or guid.combo).

0
source share

All Articles