-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: NonUniqueObjectException when updating deserialized object
PostPosted: Mon Nov 21, 2005 7:36 pm 
Beginner
Beginner

Joined: Mon Nov 21, 2005 6:38 pm
Posts: 30
Location: New Zealand
Hi,

Sorry if this is covered elsewhere, but I have searched the forums, and read thru Hibernate In Action, without finding a solution.

I have a web service that retrieves objects from a database, serializes them (using binaryformatter) and passes them to a client for its use. The NH session is stored in the HTTPSession for reuse. Code is as follows:

Code:
public byte[] GetEmployeeList()
    {
      ISession hibernateSession = IISSession[Keys.HibernateSessionKey] as ISession;

      ArrayList tmpArray = new ArrayList(hibernateSession.CreateCriteria(typeof (Employee)).List());


      IISSession[Keys.HibernateSessionKey] = hibernateSession;
      return base.Serialization.Serialize(tmpArray);
    }



My problem is that when the objects are sent back to the web service for persisting, the call to the .Update method below throws a NonUniqueObjectException.

Code:
public bool SaveEmployees(byte[] serializedData)
    {
      bool successState = true;

      ArrayList employeeList = base.Serialization.Deserialize(serializedData) as ArrayList;

      Debug.Assert(IISSession != null);

      ISession hibernateSession = IISSession[Keys.HibernateSessionKey] as ISession;
      Debug.Assert(hibernateSession != null);

      ITransaction t = hibernateSession.BeginTransaction();

      try
      {
        foreach (Employee inEmp in employeeList)
        {
          hibernateSession.Update(inEmp);
        }
        t.Commit();
      }
      catch (HibernateException ex)
      {
        t.Rollback();
        throw ex;
      }

      IISSession[Keys.HibernateSessionKey] = hibernateSession;

      return successState;
    }


As the HIA book suggests, I have overridden the == operator (and the .Equals method) for the class, so that the object held in the NH session will be the same as the deserialized object being passing to the Update method.

Debugging my SaveEmployees method shows that the == returns true when the objects are tested for equality, so I am at a loss as to why NH thinks that the object passed into Update is "a different object with the same identifier value".

In case it is of interest, the mapping for the Employee class is

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="EAF.Common.Persistence.Employee, EAF.Common" table="dbo.Employees">
      <id name="EmployeeID" column="EmployeeID" type="integer" unsaved-value="0" >
         <generator class="native"/>
      </id>
      <version name="Version" column="Version" type="Int32"/>
      <property name="LastName" column="LastName" type="String" length="20"/>
      <property name="FirstName" column="FirstName" type="String" length="10" />
      <property name="Title" column="Title" type="String" length="30"/>
      <property name="TitleOfCourtesy" column="TitleOfCourtesy" type="String" length="25"/>
      <property name="BirthDate" column="BirthDate" type="date"/>
      <property name="HireDate" column="HireDate" type="DateTime"/>
      <property name="Address" column="Address" type="String" length="60"/>
      <property name="City" column="City" type="String" length="15"/>
      <property name="Region" column="Region" type="String" length="15"/>
      <property name="PostalCode" column="PostalCode" type="String" length="10"/>
      <property name="Country" column="Country" type="String" length="15"/>
      <property name="HomePhone" column="HomePhone" type="String" length="24"/>
      <property name="Extension" column="Extension" type="String" length="4"/>
      <property name="Notes" column="Notes" type="String" />
   </class>
</hibernate-mapping>


thanks for your help.
Dean.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 10:42 pm 
Beginner
Beginner

Joined: Mon Nov 21, 2005 6:38 pm
Posts: 30
Location: New Zealand
Problem is solved by using the SaveOrUpdateCopy method rather than the Update/SaveOrUpdate methods.

I don't even need to override the == operator for my objects, which makes my coding simpler :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.