views:

73

answers:

2

I have a simple Parent/Child relationship between a Person object and an Address object. The Person object exists in the DB. After doing a Get on the Person, I add a new Address object to the Address sub-object list of the parent, and do some other updates to the Person object. Finally, I do an Update on the Person object. With a SQL trace window, I can see the update to the Person object to the Person table and the Insert of the Address record to the Address table.

The issue is that, after the update is performed, the AddressId (primary key on the Address object) is still set to 0, which is what it defaults to when you first initialize the Address object. I have verified that when I do an Add, this value is set correctly. Is this a known issue when trying to add sub-objects as part of an NHibernate UPDATE? Sample code and mapping files are below

Thanks

<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; 
A: 

They are displayed now. Sorry about that

BMZ
A: 

Try:

    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);
    addr.Person = updatePerson

A common practice is to expose Add and Remove methods for managing collections, e.g.

public void AddAddress(Address addr)
{
    PersonAddresses.Add(addr);
    addr.Person = this;
}
Jamie Ide
Thanks for the tip. Unfortunately, the updatePerson.PersonAddresses[0].AddressId = 0 after the Update/Add is done
BMZ