我在Person對象和Address對象之間有一個簡單的父/子關係。 Person對象存在於數據庫中。在完成Get on Person之後,我將一個新的Address對象添加到父對象的Address子對象列表中,並對Person對象進行一些其他更新。最後,我對Person對象進行更新。使用SQL跟蹤窗口,我可以將Person對象的更新看到Person表,並將地址記錄的插入到地址表中。在NHibernate中,如何在更新父對象時添加子對象?
問題是,執行更新後,AddressId(Address對象上的主鍵)仍然設置爲0,這是您首次初始化Address對象時默認設置的值。我已經證實,當我做一個添加,這個值設置正確。當嘗試添加子對象作爲NHibernate UPDATE的一部分時,這是一個已知問題嗎?示例代碼和映射文件低於
感謝
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="BusinessEntities.Wellness.Person,BusinessEntities.Wellness" table="Person" lazy="true" dynamic-insert="true" dynamic-update="false">
<id name="Personid" column="PersonID" type="int">
<generator class="native" />
</id>
<version type="binary" generated="always" name="RecordVersion" column="`RecordVersion`"/>
<property type="int" not-null="true" name="Customerid" column="`CustomerID`" />
<property type="AnsiString" not-null="true" length="9" name="Ssn" column="`SSN`" />
<property type="AnsiString" not-null="true" length="30" name="FirstName" column="`FirstName`" />
<property type="AnsiString" not-null="true" length="35" name="LastName" column="`LastName`" />
<property type="AnsiString" length="1" name="MiddleInitial" column="`MiddleInitial`" />
<property type="DateTime" name="DateOfBirth" column="`DateOfBirth`" />
<bag name="PersonAddresses" inverse="true" lazy="true" cascade="all">
<key column="PersonID" />
<one-to-many class="BusinessEntities.Wellness.PersonAddress,BusinessEntities.Wellness"/
</bag>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="BusinessEntities.Wellness.PersonAddress,BusinessEntities.Wellness" table="PersonAddress" lazy="true" dynamic-insert="true" dynamic-update="false">
<id name="PersonAddressId" column="PersonAddressID" type="int">
<generator class="native" />
</id>
<version type="binary" generated="always" name="RecordVersion" column="`RecordVersion`" />
<property type="AnsiString" not-null="true" length="1" name="AddressTypeid" column="`AddressTypeID`" />
<property type="AnsiString" not-null="true" length="60" name="AddressLine1" column="`AddressLine1`" />
<property type="AnsiString" length="60" name="AddressLine2" column="`AddressLine2`" />
<property type="AnsiString" length="60" name="City" column="`City`" />
<property type="AnsiString" length="2" name="UsStateId" column="`USStateID`" />
<property type="AnsiString" length="5" name="UsPostalCodeId" column="`USPostalCodeID`" />
<many-to-one name="Person" cascade="none" column="PersonID" />
</class>
</hibernate-mapping>
Person newPerson = new Person();
newPerson.PersonName = "John Doe";
newPerson.SSN = "111111111";
newPerson.CreatedBy = "RJC";
newPerson.CreatedDate = DateTime.Today;
personDao.AddPerson(newPerson);
Person updatePerson = personDao.GetPerson(newPerson.PersonId);
updatePerson.PersonAddresses = new List<PersonAddress>();
PersonAddress addr = new PersonAddress();
addr.AddressLine1 = "1 Main St";
addr.City = "Boston";
addr.State = "MA";
addr.Zip = "12345";
updatePerson.PersonAddresses.Add(addr);
personDao.UpdatePerson(updatePerson);
int addressID = updatePerson.PersonAddresses[0].AddressId;
這是您的C#代碼,並且未提供映射。請提供您的XML映射文件,以便我們可以幫助您。 – 2010-05-27 15:14:51