1

I have a project by NHibernate implementation and using Lazy Loading. I have two class in this project : Person and Family. Relation between Those two is aggregation, is mean a Person has a list of Family. Mapping is :

  <class name="Person" table="Person_Person" >

    <id name="Id" type="Int64" unsaved-value="0">
      <generator class="native" />
    </id>

    <bag name="Families" inverse="true" table="Person_Family" cascade="all-delete-orphan" >
      <key column="Person_id_fk"/>
      <one-to-many class="Domain.Entities.Family,Domain.Entities"/>
    </bag>

  </class>

In this project, I Get a person by ID then update a family of families person.

Person person = SessionInstance.Get<Person>(id);
foreach (Family fam in person.Families)
    if (fam.Name == "Jaun")
        {
        fam.Code = 100;
        SessionInstance.Update(fam);
        }

The family not updated, Because throw a exception by this message : `a different object with the same identifier value was already associated with the session: 193, of entity: Domain.Entities.Family

How can i update a family of person?

Ehsan
  • 3,431
  • 8
  • 50
  • 70

2 Answers2

0

try to update person object instead of family object.

Person person = SessionInstance.Get<Person>(id);
foreach (Family fam in person.Families)
    if (fam.Name == "Jaun")
        {
        fam.Code = 100;
        }
SessionInstance.Update(person);
Zohaib
  • 7,026
  • 3
  • 26
  • 35
  • I tested your proposal, But not resolved my problem. still throw same exception. – Ehsan Nov 01 '11 at 05:20
  • I guess then u need to make sure that same person object, that is with same id, should not be elsewhere loaded in session. @ehsanzeynali – Zohaib Nov 01 '11 at 05:24
  • I'm not sure what that means? Naturally person loaded in session Becuase i need to families of person. By this pieces of up code `Person person = SessionInstance.Get(id); `. – Ehsan Nov 01 '11 at 06:04
  • post ur complete code, within session starting point and session ending point. @ehsanzeynali – Zohaib Nov 01 '11 at 06:19
0

In your case here you do not need to call Update. You just need to flush the session. In your case I would do something like this:

using (ITransaction transaction = SessionInstance.BeginTransaction())
{
    foreach (Family fam in person.Families)
    {
        if (fam.Name == "Jaun")
        {
            fam.Code = 100;
        }
    }

    transaction.Commit();
}

Or you can do something like this:

foreach (Family fam in person.Families)
{
    if (fam.Name == "Jaun")
    {
        fam.Code = 100;
    }
}

SessionInstance.Flush();

ISession.Update() is meant for updating detached objects. In your case the object is not detached. You should read the following 2 sections in the NHibernate documenation to have a better understanding of this:

http://www.nhforge.org/doc/nh/en/index.html#manipulatingdata-updating-insession
http://www.nhforge.org/doc/nh/en/index.html#manipulatingdata-updating-detached

Cole W
  • 15,123
  • 6
  • 51
  • 85