Dot-Net

NHibernate不堅持多對多關係

  • July 8, 2011

我目前使用 NHibernate 作為我的數據訪問層,使用 Fluent NHibernate 為我創建映射文件。我有兩個類,TripItem 和 TripItemAttributeValue,它們之間有一個多對多的關係。

映射如下:

public class TripItemMap : ClassMap<TripItem2>
{
   public TripItemMap()
   {
       WithTable("TripItemsInt");
       NotLazyLoaded();

       Id(x => x.ID).GeneratedBy.Identity().WithUnsavedValue(0);
       Map(x => x.CreateDate, "CreatedOn").CanNotBeNull();
       Map(x => x.ModifyDate, "LastModified").CanNotBeNull();

       /* snip */

       HasManyToMany<TripItemAttributeValue>(x => x.Attributes).AsBag()
           .WithTableName("TripItems_TripItemAttributeValues_Link")
           .WithParentKeyColumn("TripItemId")
           .WithChildKeyColumn("TripItemAttributeValueId")
           .LazyLoad();
   }
}

public class TripItemAttributeValueMap : ClassMap<TripItemAttributeValue>
{
   public TripItemAttributeValueMap()
   {
       WithTable("TripItemAttributeValues");

       Id(x => x.Id).GeneratedBy.Identity();
       Map(x => x.Name).CanNotBeNull();

       HasManyToMany<TripItem2>(x => x.TripItems).AsBag()
           .WithTableName("TripItems_TripItemAttributeValues_Link")
           .WithParentKeyColumn("TripItemAttributeValueId")
           .WithChildKeyColumn("TripItemId")
           .LazyLoad();
   }
}

在我的應用程序的某個時刻,我從數據庫中獲取現有屬性,將它們添加到tripItem.Attributes,然後保存tripItem 對象。最後,TripItems_TripItemAttributeValues_Link 永遠不會得到任何新記錄,導致關係沒有被持久化。

如果有幫助,這些是 Fluent NHibernate 為這些類生成的映射文件:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="ETP.Core" namespace="ETP.Core.Domain">
 <class name="TripItem2" table="TripItemsInt" xmlns="urn:nhibernate-mapping-2.2" lazy="false">
   <id name="ID" column="ID" type="Int32" unsaved-value="0">
     <generator class="identity" />
   </id>
   <property name="CreateDate" column="CreatedOn" type="DateTime" not-null="true">
     <column name="CreatedOn" />
   </property>
   <property name="ModifyDate" column="LastModified" type="DateTime" not-null="true">
     <column name="LastModified" />
   </property>
   <bag name="Attributes" lazy="true" table="TripItems_TripItemAttributeValues_Link">
     <key column="TripItemId" />
     <many-to-many column="TripItemAttributeValueId" class="ETP.Core.Domain.TripItemAttributeValue, ETP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
   </bag>
 </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="ETP.Core" namespace="ETP.Core.Domain">
 <class name="TripItemAttributeValue" table="TripItemAttributeValues" xmlns="urn:nhibernate-mapping-2.2">
   <id name="Id" column="Id" type="Int32">
     <generator class="identity" />
   </id>
   <property name="Name" column="Name" length="100" type="String" not-null="true">
     <column name="Name" />
   </property>
   <bag name="TripItems" lazy="true" table="TripItems_TripItemAttributeValues_Link">
     <key column="TripItemAttributeValueId" />
     <many-to-many column="TripItemId" class="ETP.Core.Domain.TripItem2, ETP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
   </bag>
 </class>
</hibernate-mapping>

我在這裡做錯了什麼?

@efdee

我遇到了同樣的問題,花了將近兩天的時間。我有一個多對多的關係,並且連結表也沒有更新。我是 NHibernate 的新手,只是想學習它,所以對我所說的一切持保留態度。

好吧,事實證明它不是 Fluent NHibernate,也不是映射,但我不明白 NHibernate 如何與多對多一起工作。在多對多關係中,如果兩個實體上的集合都沒有填充,NHibernate 不會將數據持久化到連結表中。

假設我在多對多關係中有這個實體:


partial class Contact
{
  public string ContactName {get; set;}
  public IList Locations {get; set;}

}

partial class Location
{
  public string LocationName {get; set;}
  public string LocationAddress {get;set;}
  public IList Contacts {get;set;}
}

當我將位置添加到 Contact.Locations 時,我必須確保該聯繫人也存在於 location.Contacts 中。

所以要添加一個位置,我在我的 Contact 類中有這個方法。


public void AddLocation(Location location)
       {
           if (!location.Contacts.Contains(this))
           {
               location.Contacts.Add(this);
           }
           Locations.Add(location);
       }

這似乎解決了我的問題,但就像我說的我只是拿起 NHibernate 並學習它,可能有更好的方法。如果有人有更好的解決方案,請發布。

這是讓我檢查兩個集合的文章:http: //www.coderanch.com/t/217138/Object-Relational-Mapping/link-table-of-ManyToMany-annotation

引用自:https://stackoverflow.com/questions/400097