views:

116

answers:

1

Sorry if this is a dupe, couldn't find it but didnt really know what to search for, anyway...

I have three classes, Parent, Child and Other

Parent has many Child where child has a Parent_Id column

Other holds a reference to a Child through a Child_Id column

When I delete a Parent, I also want to delete all the associated Child objects. If these Child objects are referenced by any Other classes, I want their (the Other objects) Child_Id references to be nullified.

What cascade rules do I need on the two relationships?

Also, will NHibernate update entities in-memory as well as in the database?

I.e. if I have a bunch of Parent, Child and Other in memory (i.e. loaded from db, not transient) and tell NH to delete a Parent, what will happen? I assume the Parent and Child objects will become transient? What will happen to the Child property of any Other objects?

Edit: when using All-Delete-Orphan, what classes an object as an orphan? In the example above, is a Child an orphan if its parent Parent is deleted? Does the reference from Other matter when considering an entity as orphaned?

Thanks

+1  A: 

NH does not update any of your entities in memory (except of Ids and Versions). NH is not responsible to manage the relations of you entities. It just persists what you did in memory to the database.

From this point of view it should become easier to understand.

cascade="delete" means that when the parent is deleted, the child gets deleted as well. cascade="delete-orphan" means, that additionally, the child is even deleted if no parent references it anymore. This, of course, only works if the child is in the session.

The deleted instance gets transient in memory. References to the transient instance (from Other) will cause an exception. AFAIK, you need to remove reference to deleted instances by yourself. You probably can make it implicit by some tricks, but I doubt that this will be clean. It's business logic.

For parent-child relations, cascade="all-delete-orphan" is appropriate.

For regular reference I prefer cascade="none".

There is a great explanation by Ayende Rahien

Stefan Steinegger
Thanks for the clarification. If i had a one-to-many from `Child` to `Other` i could set cascade to save-update and that would give me the functionality i required, right? except i dont want the have a bi-directional relationship, i only want to be able to go from `Other` to `Child`, not the other way.
Andrew Bullock
I wouldn't save-update Other because you save the child. Assuming that Other is an independent entity, that is created and deleted independent from child, it should not be created implicitly by creating a child. For me this is kind of a best-practice. I don't why you think this would solve your problem. You need to remove the reference to the child from the Other anyway. Of course, you will find the referencing Others easier.
Stefan Steinegger
hmm no you are right, ok fair enough. so does that mean i need to load all `Other`s into memory and then nullify their `Child` references? seems expensive :s
Andrew Bullock
In NH 2.1, you can update using HQL.
Stefan Steinegger
ok, good idea. ta :)
Andrew Bullock