views:

923

answers:

2

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.

A: 

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>
Michal
A: 

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