2010-10-22 62 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

相关问题