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.