nhibernate many to many deletes

Posted by asi farran on Stack Overflow See other posts from Stack Overflow or by asi farran
Published on 2010-04-29T11:40:03Z Indexed on 2010/04/29 11:47 UTC
Read the original article Hit count: 430

I have 2 classes that have a many to many relationship. What i'd like to happen is that whenever i delete one side ONLY the association records will be deleted with no concern which side i delete.

simplified model:

classes:

class Qualification
{
     IList<ProfessionalListing> ProfessionalListings 
}

class ProfessionalListing
{
     IList<Qualification> Qualifications

     void AddQualification(Qualification qualification)
     {
        Qualifications.Add(qualification);
        qualification.ProfessionalListings.Add(this);
     } 
}

fluent automapping with overrides:

void Override(AutoMapping<Qualification> mapping)
{
     mapping.HasManyToMany(x => x.ProfessionalListings).Inverse();
}

void Override(AutoMapping<ProfessionalListing> mapping)
{
    mapping.HasManyToMany(x => x.Qualifications).Not.LazyLoad();
}

I'm trying various combinations of cascade and inverse settings but can never get there. If i have no cascades and no inverse i get duplicated entities in my collections. Setting inverse on one side makes the duplication go away but when i try to delete a qualification i get a 'deleted object would be re-saved by cascade'.

How do i do this?

Should i be responsible for clearing the associations of each object i delete?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about many-to-many