NHibernate多对多映射以及映射表中的其他列 [英] NHibernate Many to many mapping with additional columns in the mapping table

查看:79
本文介绍了NHibernate多对多映射以及映射表中的其他列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个表Person和Address.我有一个映射表PersonAddressMap,其中包含除了两个外键(没有显式主键列)之外的其他列IsCurrent.我创建了三个映射类,分别用于Person,Address和PersonAddressMap.我需要访问一个Person的所有地址,还需要从Address访问所有具有特定地址的Person,因此Person和Address类中的PersonAddressMap是一对多的.现在的问题是双向关联正在创建一个无限循环.如果删除其中一个关联,则负载工作正常,但是保存"未插入映射表中(例如:如果我只有一个从Person到PersonAddressMap的关联,并尝试插入具有新地址的新Person,则只有Person是插入.).映射文件如下所示,

There are two tables Person and Address. I have a mapping table PersonAddressMap which contains an additional column IsCurrent other than the two foreign keys (no explicit primary key column). I have created three mapping classes one each for Person, Address and PersonAddressMap. I need to access all the Addresses of a Person and also access all the Persons who have a particular address from Address, so I have a one-to-many to PersonAddressMap in both the Person and Address class. Now the problem is bi-directional association is creating an infiniteloop. If I remove one of the associations, the load is working fine, but Save is not inserting into the mapping table (Eg: If I have only one association from Person to PersonAddressMap and try to insert a new Person with new address then only Person is getting inserted.). The mapping files look like this,

  <class name="Person" table="Person">

    <id name="Id">  
      <column name="Id" sql-type="int" not-null="true" />
      <generator class="identity" />
    </id>
    <property name="Name" column="Name"/>
    <bag name="AddressDetails" table="PersonAddressMap" cascade="all" >
      <key column="PersonId" />
      <one-to-many class="PersonAddressMap" />
    </bag>
  </class>

  <class name="Address" table="Address">

    <id name="Id">  
      <column name="Id" sql-type="int" not-null="true" />
      <generator class="identity" />
    </id>
    <property name="City" column="City"/>
    <bag name="PersonDetails" table="PersonAddressMap" cascade="all" >
      <key column="AddressId" />
      <one-to-many class="PersonAddressMap" />
    </bag>
  </class>

  <class name="Job" table="Job" lazy="false">
    <composite-id>
      <key-many-to-one column="PersonId" name="Person" />
      <key-many-to-one column="AddressId" name="Address" />
    </composite-id>
    <property name="IsCurrent" column="IsCurrent"/>
  </class>

谢谢.

推荐答案

当我使用inverse ='true'时,不会插入联接表值(即PersonAddressMap).

When I use inverse='true' the join table values (ie. PersonAddressMap) do not get inserted.

如果我设置inverse ='false',则会插入它们,但是我也看到删除语句在sql中出现

If I set inverse='false', they get inserted, but I also see delete statements occur in sql

这篇关于NHibernate多对多映射以及映射表中的其他列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆