views:

1604

answers:

1

I use ASP.Net with NHibernate accessing a Pgsql database.

For some of our Objects, we use NHibernate bags, which map to List objects in our application. Sometimes we have issues with needing to refresh the objects through NHibernate when we update anything to do with the lists in the database.

<bag name="Objects" inverse="true" lazy="true" generic="true" >
   <key column="object_id" />
   <one-to-many class="Object" />
</bag>

Above is a sample of the code I use for our bags.

I was wondering if anyone else came across this issue anywhere, and what you do to work around it?

+2  A: 

Have you tried NHibernate cascades, such as save-update?

You are able to tell NHibernate to automatically traverse an entity's associations, and act according to the cascade option. For instance, adding an unsaved entity to a collection with save-update cascade will cause it to be saved along with its parent object, without any need for explicit instructions on our side.

Here is what each cascade option means:

  • none - do not do any cascades, let the users handle them
  • save-update - when the object is saved/updated, check the associations and save/update any object that requires it (including save/update the associations in many-to-many scenario).
  • delete - when the object is deleted, delete all the objects in the association.
  • delete-orphans - when the object is deleted, delete all the objects in the association. In addition to that, when an object is removed from the association and not associated with another object (orphaned), also delete it.
  • all - when an object is saved/updated/deleted, check the associations and save/update/delete all the objects found.
  • all-delete-orphans - when an object is saved/updated/deleted, check the associations and save/update/delete all the objects found. In addition to that, when an object is removed from the association and not associated with another object (orphaned), also delete it.

More info here: NHibernate Cascades: the different between all, all-delete-orphans and save-update

Sam Wessel