2009-06-14 98 views
0

有兩個表Person和Address。我有一個映射表PersonAddressMap其中包含一個額外的列IsCurrent除了兩個外鍵(沒有明確的主鍵列)。我爲Person,Address和PersonAddressMap創建了三個映射類。我需要訪問某個人的所有地址,並訪問所有地址中具有特定地址的人員,因此我在Person和Address類中都有一對一的PersonAddressMap。現在問題是雙向關聯正在創建一個infiniteloop。如果我刪除其中一個關聯,加載工作正常,但保存沒有插入到映射表中(例如:如果我只有一個從Person到PersonAddressMap的關聯,並嘗試插入一個具有新地址的新Person,則只有Person是插入)。映射文件看起來像這樣,NHibernate多對多映射表中的附加列

<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> 

在此先感謝。

回答

0

多對多關係總是要求您標記關係的一個終點與逆。在你的具體例子中,你可以使用Person的AddressDetails。例如

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

當我使用inverse ='true'時,連接表值(即PersonAddressMap)不會被插入。

如果我設置inverse ='false',它們會被插入,但我也會看到刪除語句出現在sql