2010-10-11 37 views
2

我有3代表這樣的傳統數據庫:
Painting of Tables http://www.bilder-hochladen.net/files/gch8-1.png
該項目表中包含一個計劃中的所有項目。 結構表定義了項目之間的關係。 父項由companyyearplanId和表中的parentItemstructure映射到companyyearplanId和表itemid限定。
子項目被companyyearplanId和表structure映射childItemcompanyyearplanId和表itemid限定。自我反思N:使用組合鍵米關係在NHibernate的

我正在尋找一種方法來使用hbm或fluent映射在nhibernate中執行n:m映射。

我想出了:

<?xml version="1.0" encoding="utf-8"?> 
<hibernate-mapping namespace="Project.Model" assembly="Project" xmlns="urn:nhibernate-mapping-2.2"> 
    <class name="Item" lazy="true" table="`item`" schema="`dbo`"> 
    <composite-id> 
     <key-property name="Company" column="`company`" /> 
     <key-property name="Year" column="`year`" /> 
     <key-property name="Planid" column="`planid`" /> 
     <key-property name="ItemId" column="`id`" /> 
    </composite-id> 
    <!-- Some other properties --> 

    <set name="Parents" table="`structure`" fetch="select"> 
     <key> 
      <column name="`company`" /> 
      <column name="`year`" /> 
      <column name="`planid`" /> 
      <column name="`parentItem`" /> 
     </key> 
     <many-to-many class="Item">   
      <column name="`company`" /> 
      <column name="`year`" /> 
      <column name="`planid`" /> 
      <column name="`id`" /> 
     </many-to-many> 
    </set> 

    <set name="Childs" table="`structure`" fetch="select"> 
     <key> 
      <column name="`company`" /> 
      <column name="`year`" /> 
      <column name="`planid`" /> 
      <column name="`childItem`" /> 
     </key> 
     <many-to-many class="Item">   
      <column name="`company`" /> 
      <column name="`year`" /> 
      <column name="`planid`" /> 
      <column name="`id`" /> 
     </many-to-many> 
    </set> 

    <many-to-one name="Plan" class="Plan" not-null="true" fetch="select"> 
     <column name="`company`" /> 
     <column name="`planid`" /> 
    </many-to-one> 
    </class> 
</hibernate-mapping> 

然而,錯誤是:Repeated column in mapping … - 所以我堅持。有什麼建議麼?

回答

0

那麼我設法解決這個問題。關鍵是要使用密鑰多到一個(KeyReference在FluentNHibernate)和,使N:M映射(因爲它不工作!):

<?xml version="1.0" encoding="utf-8"?> 
<hibernate-mapping namespace="Project.Model" assembly="Project" xmlns="urn:nhibernate-mapping-2.2"> 
<class name="Item" lazy="true" table="`item`" schema="`dbo`"> 
    <composite-id> 
    <key-many-to-one name="Plan" class="Plan"> 
     <column name="Company" column="`company`" /> 
     <column name="Planid" column="`planid`" /> 
    </key-many-to-one> 
    <key-property name="Year" column="`year`" /> 
    <key-property name="ItemId" column="`id`" /> 
    </composite-id> 
    <!-- Some other properties --> 

    <set name="Parents" table="`structure`" fetch="select"> 
    <key> 
     <column name="`company`" /> 
     <column name="`year`" /> 
     <column name="`planid`" /> 
     <column name="`parentItem`" /> 
    </key> 
    <many-to-many class="Item">   
     <column name="`company`" /> 
     <column name="`year`" /> 
     <column name="`planid`" /> 
     <column name="`id`" /> 
    </many-to-many> 
    </set> 

    <set name="Childs" table="`structure`" fetch="select"> 
    <key> 
     <column name="`company`" /> 
     <column name="`year`" /> 
     <column name="`planid`" /> 
     <column name="`childItem`" /> 
    </key> 
    <many-to-many class="Item">   
     <column name="`company`" /> 
     <column name="`year`" /> 
     <column name="`planid`" /> 
     <column name="`id`" /> 
    </many-to-many> 
    </set> 
    </class> 

    <class name="Structure" lazy="true" table="`item`" schema="`dbo`"> 
    <composite-id> 
    <key-many-to-one name="Parent" class="Item"> 
    <column name="company" /> 
    <column name="year" /> 
    <column name="planid" /> 
    <column name="parentItem" /> 
    </key-many-to-one> 
    <key-many-to-one name="Child" class="Item"> 
    <column name="company" /> 
    <column name="year" /> 
    <column name="planid" /> 
    <column name="childItem" /> 
    </key-many-to-one> 
    <key-property name="StructureId" column="structureId" /> 
</composite-id>  
</class> 

</hibernate-mapping> 

這看起來有點怪異,ESP。公司,年度和planid在結構的關鍵中是兩倍的部分。但它有效...