Further to my other question given the following classes and fluent map, is there any way to automatically cascade delete a DriversLicense when the related Person is deleted? Note, I do not want the Person class to have any knowledge of the DriversLicense class, but I also don’t want orphaned data if a Person gets deleted.
public class PersonMap : ClassMap<Person>
{
public PersonMap() { Id( x => x.Id ); }
}
public class PersonMap : ClassMap<Person>
{
public PersonMap() { Id( x => x.Id ); }
}
public class DriversLicense
{
public virtual Person Person { get; set; }
public virtual string State { get; set; }
public override bool Equals( object obj ){ ... }
public override int GetHashCode(){ ... }
}
public class DriversLicenseMap : ClassMap<DriversLicense>
{
public DriversLicenseMap()
{
UseCompositeId().WithKeyReference( x => x.Person );
Map( x => x.State );
}
}
Since you can’t cascade deletes from DriversLicense to Person, for business reasons, and you really don’t what the Person to know about the DriversLicense and you have a really good reason for this requirement then I think you could handle it in one of two ways:
Move the responsibilty for deleting the DriversLicense object further into your service or repository levels and doing what Chris suggested. Query for a Person’s DriverLicnese objects and delete them before you delete the Person entity.
You could create an Nhibernate Interceptor or EventHandler that performs deletes for you. This approach leaves your repositories and domain nice and clean and is probably the approach I would take if I couldn’t reference the DriveLicenese collection from my person object. (NHibernate Event Documentation)