代码之家  ›  专栏  ›  技术社区  ›  MoJo2600

如何使用接口将复合id与fluent nhibernate映射?

  •  3
  • MoJo2600  · 技术社区  · 15 年前

    .hbm 映射到fluent映射,并且在复合id的映射和接口的使用方面有问题

    课程内容如下:

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

    <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生成的映射如下:

    ...
     <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”属性现在指向接口,而不是指向导致错误的该接口的实现。

    2 回复  |  直到 10 年前
        1
  •  3
  •   cdmdotnet    15 年前

    尝试下载 NhGen from SourceForge

    我认为它使用的语法类似于

     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");
    
        2
  •  2
  •   MoJo2600    15 年前

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