2008-12-16 21 views
1

任何幫助,你可以給非常感激地接受。我一直在反覆研究這個問題,似乎無法找到解決方案。 。它可能是在盯着我的臉:(nhibernate,父母和孩子導致重複鍵的階級

我曾與父母和子女的列表下面的類在層次結構表中堅持

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

在休眠設置,我有:

<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> 

這導致重複鍵在層次結構表作爲密鑰對被插入了孩子和家長都。

有沒有解決這個Ø而不是在單獨的表中保留父層次結構和子層次結構?

+0

上面的映射是當前的實現,但這會嘗試在表中插入重複的parentId,childId - 爲父項和子項插入。 我試過了cascade屬性的各種設置。 當然,有一種方法可以爲PHierachy和層次結構建模而不用單獨的表格嗎? – user46702 2008-12-16 17:35:52

+0

您是否嘗試過在鍵列上指定foreign_key名稱? – Tigraine 2008-12-17 10:58:49

回答