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

发布于 2024-09-28 14:42:41 字数 1667 浏览 5 评论 0原文

我正在尝试将 .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 生成映射如下:

...
 <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 使用另一个类作为属性值?

I'm trying to switch out .hbm mappings to fluent mappings and have a problem with the mapping of composite-ids and the usage of Interfaces

the Class looks as follows:

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

our hbm mapping looks like this:

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

Please note, that we got interfaces in the Class! No I'm trying to map this with Fluent nhibernate.

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

But now Fluent generates the Mapping as follows:

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

The "Class" Attribute points now to the Interface not to the implementation of this interface which results in an error.

How can I tell Fluent nHibernate to use another class as the attribute value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

四叶草在未来唯美盛开 2024-10-05 14:42:41

尝试从 SourceForge 下载NhGen。它读取数据库模式并生成 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");

Try downloading NhGen from SourceForge. It reads database schemas and generates Fluent mappings and classes etc. While all the code might not be what you need, it should start you off in the right direction as it supports composite keys and represents them as separate classes off the main entity.

I beleive it uses a syntax similar to

 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");
如果没结果 2024-10-05 14:42:41

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

您可以在此处查看原始对话:

Tanks! But I've found the answer on my on. In fact i found a missing feature in fluent nHibernate. The feature has already been added to the dev branch by Paul Batum.

You would use it like so:

   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

You can see the original Conversation here: 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文