tags:

views:

165

answers:

2

I have parent child relationship between two entities(Parent and Child).

My Parent mapping is as follows:

<class name="Parent" table="Parents">
    ...
    <bag name="Children" cascade="all">
        <key column="ParentID"></key>
        <one-to-many class="Child"></one-to-many>
    </bag>
</class>

I would like to execute the following:

someParent.Children.Remove(someChild);

The Child class has a reference to another parent class, Type. The relationship looks like

http://yuml.me/diagram/scruffy/class/%5BParent%5D+1-%3E*%5BChild%5D%2C%20%5BType%5D+1-%3E*%5BChild%5D

Note: I apologize for the non-linked url above, I couldn't seem to get past the asterisk in the url string using Markup

Due to this relationship, when the above code is called, instead of a DELETE query, an UPDATE query is generated which removes the ParentID from the Child table(sets to null).

Is it possible to force NHibernate to delete the child record completely, when removed from the Parent.Children collection?

UPDATE

@Spencer's Solution

Very attractive solution as this is something that can be implemented in future classes. However, due to the way sessions are handled(in my particular case) in the repository pattern, this is near impossible as we would have to pass session types(CallSessionContext/WebSessionContext) depending on the application.

@Jamie's Solution

Simple and quick to implement, however I've hit another road block. My child entity looks as follows: alt text

When using the new method, NHibernate generates an update statement setting the TypeID and ParentID to null, as opposed to a single delete outright. If I missed something within the implementation, let me know as this method would be painless to move forward with.

@The One-Shot-Delete solution described here, outlines an idea dereferencing the collection to force a single delete. Same results as above however, an update statement is issued.

//Instantiate new collection and add persisted items
List<Child> children = new List<Child>();
children.AddRange(parent.Children);

//Find and remove requested items from new collection
var childrenToRemove = children
    .Where(c => c.Type.TypeID == 1)
    .ToList();

foreach (var c in childrenToRemove) { children.Remove(m); }
parent.Children = null;

//Set persisted collection to new list
parent.Children = Children;

Solution

Took a bit of digging, but Jamie's solution came through with some additional modifications. For future readers, based on my class model above:

Type mapping - Inverse = true, Cascade = all Parent mapping - Inverse = true, Cascade = all-delete-orphan

Remove methods as described in Jamie's solution works. This does produce a single delete statement per orphaned item, so there is the possibility for tuning in the future, however the end result is successful.

A: 

I don't think this is exactly possible because Hibernate has no way of knowing if the record has been orphaned. It can check if any other classes relate to the child class but then it would be assuming that it's aware of the entire DB structure which may not be the case.

You're not completely out of luck however. By using the IList interface in conjunction with a custom ICascadeDeleteChild interface you'd create you can come up with a rather seamless solution. Here are the basic steps.

  1. Create a class that inheirits IList and IList<> and call it CascadeDeleteList or something along those lines.
  2. Create a private .Net List inside this class and simply proxy the various method calls to this List.
  3. Create an Interface called ICascadeDeleteChild and give it a method Delete()
  4. Under the Delete method for your CascadeDeleteList check the type of the object that is to be deleted. If it is of type ICascadeDeleteChild then call it's Delete method.
  5. Change your Child class to implement the ICascadeDeleteChild interface.

I know it seems like a pain but once this is done these interfaces should be simple to port around.

Spencer Ruport
I believe this may have sparked me onto the right path, the road blocks being: setting this up in a repository pattern(ie, in this case the child Delete method would call its own repository method) and also being able to use this with different session contexts(ie, CallSessionContext vs WebSessionContext)
Alexis Abril
A: 

Instead of exposing the IList<Child>, control access to the collection through a method:

RemoveChild(Child child)
{
    Children.Remove(child);
    child.Parent = null;
    child.Type.RemoveChild(child);
}

Type.RemoveChild would look similar but you would have to be careful to not put it into an infinite loop calling each other's RemoveChild methods.

Jamie Ide