在nhibernate中使用复合键的自反现n:m关系

时间:2010-10-11 07:05:19

标签: nhibernate fluent-nhibernate nhibernate-mapping

我有一个遗留数据库,有3个这样的表:
Painting of Tables http://www.bilder-hochladen.net/files/gch8-1.png
Items表包含计划中的所有项目。 Structure表定义了项之间的关系。 父项目由表格company的{​​{1}},yearplanIdparentItem定义,映射到structurecompany,表year的{​​{1}}和planId 子项目由表格id的{​​{1}},itemcompanyyear定义,映射到planIdchildItem,表structure的{​​{1}}和company

我正在寻找一种使用hbm或fluent映射在nhibernate中进行n:m映射的方法。

我想出了:

year

然而错误是:planId - 所以我被卡住了。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。关键是使用key-many-to-one(FluentNHibernate中的KeyReference)和 not 来进行n:m映射(因为它不起作用!):

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="Project.Model" assembly="Project" xmlns="urn:nhibernate-mapping-2.2">
<class name="Item" lazy="true" table="`item`" schema="`dbo`">
  <composite-id>
    <key-many-to-one name="Plan" class="Plan">
      <column name="Company" column="`company`" />
      <column name="Planid" column="`planid`" />
    </key-many-to-one>
    <key-property name="Year" column="`year`" />
    <key-property name="ItemId" column="`id`" />
  </composite-id>  
  <!-- Some other properties -->

  <set name="Parents" table="`structure`" fetch="select">
    <key>
        <column name="`company`" />
        <column name="`year`" />
        <column name="`planid`" />
        <column name="`parentItem`" />
    </key>
    <many-to-many class="Item">        
        <column name="`company`" />
        <column name="`year`" />
        <column name="`planid`" />
        <column name="`id`" />
    </many-to-many>
  </set>

  <set name="Childs" table="`structure`" fetch="select">
    <key>
        <column name="`company`" />
        <column name="`year`" />
        <column name="`planid`" />
        <column name="`childItem`" />
    </key>
    <many-to-many class="Item">        
        <column name="`company`" />
        <column name="`year`" />
        <column name="`planid`" />
        <column name="`id`" />
    </many-to-many>
  </set>
  </class>

  <class name="Structure" lazy="true" table="`item`" schema="`dbo`">
    <composite-id>
  <key-many-to-one name="Parent" class="Item">
    <column name="company" />
    <column name="year" />
    <column name="planid" />
    <column name="parentItem" />
  </key-many-to-one>
  <key-many-to-one name="Child" class="Item">
    <column name="company" />
    <column name="year" />
    <column name="planid" />
    <column name="childItem" />
  </key-many-to-one>
  <key-property name="StructureId" column="structureId" />
</composite-id>      
</class>

</hibernate-mapping>

这看起来有些奇怪,尤其是公司,年份和计划在结构的关键中是两次的部分。但它有效......