2010-10-22 65 views
3

我試圖.hbm映射切換出流暢的映射,並具有複合id的映射和接口如何使用接口將複合id與流暢的nhibernate進行映射?

使用類看起來如下問題:

public class ClassWithCompositeId { 
    public virtual IKeyOne KeyOne { get; set; } 
    public virtual IKeyTwo KeyTwo { get; set; } 
} 

我們HBM映射看起來像這樣:

<hibernate-mapping ...> 
    <class name="ClassWithCompositeId" table="t_classwithcompositeid"> 
     <composite-id>  
     <key-many-to-one name="KeyOne" column="colkeyone" class="company.namespace.boSkillBase, BL_Stammdaten" /> 
     <key-many-to-one name="KeyTwo" column="colkeytwo" class="boQualifikation" />   
     </composite-id> 
</hibernate-mapping> 

請注意,我們有類中的接口!不,我試圖用Fluent nhibernate來映射它。

Map { 
    public ClassWithCompositeIdMap() { 
     CompositeId() 
      .KeyReference(x => x.KeyOne, "colkeyone") 
      .KeyReference(x => x.KeyTwo, "colkeytwo"); 
      ... 
    } 
} 

但現在流利生成映射如下:

... 
<composite-id mapped="false" unsaved-value="undefined"> 
     <key-many-to-one name="KeyOne" class="company.namespace.IKeyOne, Interfaces, Version=0.1.4.3379, Culture=neutral, PublicKeyToken=null"> 
     <column name="colkeyone" /> 
     </key-many-to-one> 
     <key-many-to-one name="KeyTwo" class="company.namespace.IKeyTwo, Interfaces, Version=0.1.4.3379, Culture=neutral, PublicKeyToken=null"> 
     <column name="colkeytwo" /> 
     </key-many-to-one> 
    </composite-id> 
... 

「類」現在屬性指向接口不是本接口,這導致錯誤的執行。

我該如何告訴Fluent nHibernate使用另一個類作爲屬性值?

+0

對不起,類boQualifikation和boSkill應IKeyOne和IKeyTwo實施 – MoJo2600 2010-10-22 13:15:28

回答

2

坦克語法!但我已經找到了答案。事實上,我在流利的nHibernate中發現了一個缺失的功能。該功能已被Paul Batum添加到開發分支。

你會使用它,像這樣:

Map { 
     public ClassWithCompositeIdMap() { 
      CompositeId() 
       .KeyReference(x => x.KeyOne, k => 
        k.Type<KeyOneImplementation>(), "colkeyone") 
       .KeyReference(x => x.KeyTwo, k => 
        k.Type<KeyTwoImplementation>(), "colkeytwo"); 
      ... 
     } 
    } 

http://github.com/paulbatum/fluent-nhibernate/tree/dev

你可以看到原來的談話在這裏:http://support.fluentnhibernate.org/discussions/help/349-how-to-map-a-composite-id-when-using-interfaces-or-how-to-change-the-class-attribute-in-the-key-many-to-one-tag

3

嘗試下載NhGen from SourceForge。它讀取數據庫模式並生成Fluent映射和類等等。雖然所有代碼可能不是您所需要的,但它應該從正確的方向啓動您,因爲它支持組合鍵並將它們表示爲與主實體分離的獨立類。

我相信這使用類似於

CompositeId() 
      .ComponentCompositeIdentifier(x => x.Key, "Namespace.Key, Assembly") 
      .KeyProperty(x => x.Key.Id1, "Id1") 
      .KeyProperty(x => x.Key.Id2, "Id2") 
      .KeyProperty(x => x.Key.Id3, "Id3"); 
+0

NhGen聽起來不錯,但在SF:上沒有文件 – Cocowalla 2011-06-29 08:15:40

相關問題