NHibernate Many to many mapping with additional columns in the mapping table

梦想与她 提交于 2019-12-13 06:32:23

问题


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>

Thanks in advance.


回答1:


many-to-many relationships ALWAYS require that you mark one end point of the relation with inverse. In your sepecific example you could use the AddressDetails of Person. e.g.

<bag name="AddressDetails" table="PersonAddressMap" cascade="all" inverse="true">
  <key column="PersonId" />
  <one-to-many class="PersonAddressMap" />
</bag>



回答2:


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

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



来源:https://stackoverflow.com/questions/992895/nhibernate-many-to-many-mapping-with-additional-columns-in-the-mapping-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!