views:

20

answers:

1

Hi , i have 2 entities : ( person ) & (Address)

with follwing mapping :

 <class name="Adress" table="Adress" lazy="false">
   <id name="Id" column="Id">
    <generator class="native" />
   </id>
   <many-to-one name="Person" class="Person">
     <column name="PersonId" />
    </many-to-one>
 </class>

 <class name="Person" table="Person" lazy="false">
   <id name="PersonId" column="PersonId">
     <generator class="native" />
   </id>
   <property name="Name" column="Name" type="String" not-null="true" />
   <set name="Adresses" lazy="true" inverse="true" cascade="save-update">
     <key>
       <column name="PersonId" />
     </key>
     <one-to-many class="Adress" />
    </set>
  </class>

my propblem is that when i set Adrees.Person with new object of person ,The collection person.Adresses doesn't update itself .

should i update every end role of the association to be updated in the two both?

another thing : if i updated the Fk manually like this : Adress.PersonId it doesn't break or change association. does this is Nhibernte behavior ?

thanks in advance , i am waiting for your experiencies

A: 

The person.Addresses will not be updated by itself. You should do it like this:

address.setPerson(person);
person.getAddresses().add(address);

Alternatively, because Person.Addresses is marked with inverse="true" in your example, you could execute only

address.setPerson(person);

then flush the session and obtain the same person from the database. Its Addresses collection should be updated with new address.

This article (http://simoes.org/docs/hibernate-2.1/155.html) describes 'inverse' attribute very nicely. The article is for Hibernate, not NHibernate, but I believe it doesn't matter here.

denis_k