有两个表 Person 和 Address。我有一个映射表 PersonAddressMap ,其中包含一个附加列 IsCurrent 而不是两个外键(没有显式主键列)。我为 Person、Address 和 PersonAddressMap 创建了三个映射类。我需要访问一个人的所有地址,还需要从地址访问所有具有特定地址的人,所以我在 Person 和 Address 类中都有一个与 PersonAddressMap 的一对多。现在的问题是双向关联正在创建一个无限循环。如果我删除其中一个关联,负载工作正常,但 Save 没有插入到映射表中(例如:如果我只有一个从 Person 到 PersonAddressMap 的关联并尝试插入一个具有新地址的新 Person 那么只有 Person 是被插入。)。
<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>
提前致谢。