Nhibernate, a parent-child class that causes duplicate keys

any help you can give is greatly appreciated. I have looked at this problem over and over and cannot find a solution. He's probably looking at me in the face :(

I have the following class with a list of parents and children to be stored in a hierarchy table.

class Item  
{  
    public virtual int Id {get;set;}  
    public virtual List`<Item`> Parents {get;set;}  
    public virtual List`<Item`> Children {get;set;}  
}  

      

In hibernation settings, I have:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
  assembly="Pl.Components" namespace="Pl.Components"`>  
    <class name="Pl.Components.Item, Pl.Components" table="Item">  
        <id name="Id" type="Int32"  column="Id" >  
            <generator class="identity" />  
        </id>  
        <bag name="Parents" lazy="true" table="Hierarchy" cascade="save-update">  
            <key column="ChildId"/>  
            <many-to-many class="Item" column="ParentId" />  
        </bag>  
        <bag name="Children" lazy="true" table="Hierarchy" cascade="delete">  
            <key column="ParentId"/>  
            <many-to-many class="Item" column="ChildId"/>  
        </bag`>  
    </class>  
</hibernate-mapping>  

      

This results in duplicate keys in the hierarchy table as key pairs are inserted for both the child and parent.

Is there any solution for this purpose other than persistent parent hierarchy and child hierarchy in separate tables?

+1


source to share


1 answer


You need to mark one of the bags as inverse = "true". See http://www.hibernate.org/hib_docs/nhibernate/html/collections.html#collections-bidirectional



+2


source







All Articles