【问题标题】:How to map composite-id with fluent nhibernate using an interface?如何使用接口将复合ID映射到流利的nhibernate?
【发布时间】:2011-04-29 04:01:29
【问题描述】:

我正在尝试将 .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>

请注意,我们在类中有接口!不,我正在尝试使用 Fluent 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>
...

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

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

【问题讨论】:

  • 对不起,boQualifikation 和 boSkill 类应该是 IKeyOne 和 IKeyTwo 的实现

标签: nhibernate interface fluent-nhibernate fluent


【解决方案1】:

尝试下载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");

【讨论】:

  • NhGen 听起来不错,但 SF 上没有它的文件 :(
【解决方案2】:

坦克!但我已经在我的网站上找到了答案。事实上,我在 fluent 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多