0

有两个表 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>

提前致谢。

4

2 回答 2

0

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

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

于 2009-08-17T16:27:10.810 回答
0

多对多关系总是要求你用逆标记关系的一个端点。在您的具体示例中,您可以使用 Person 的 AddressDetails。例如

<bag name="AddressDetails" table="PersonAddressMap" cascade="all" inverse="true">
  <key column="PersonId" />
  <one-to-many class="PersonAddressMap" />
</bag>
于 2009-06-16T01:55:01.597 回答