如何使用接口映射composite-id和流畅的nhibernate?

时间:2010-10-22 13:12:09

标签: nhibernate interface fluent-nhibernate fluent

我正在尝试将.hbm映射切换为流畅的映射,并且对复合ID的映射和接口的使用存在问题

该类看起来如下:

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>

请注意,我们在课堂上有接口!不,我试图用流利的nhibernate来映射它。

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

但现在Fluent按如下方式生成Mapping:

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

“Class”属性现在指向接口而不是此接口的实现,这会导致错误。

如何告诉Fluent nHibernate使用另一个类作为属性值?

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分:2)

坦克!但我已经找到了答案。事实上,我发现流利的nHibernate缺少一个功能。该功能已被Paul Batum添加到dev分支。

你会像这样使用它:

   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