-->
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.  [ 3 posts ] 
Author Message
 Post subject: org.hibernate.NonUniqueObjectException
PostPosted: Tue Jan 17, 2006 12:36 am 
Newbie

Joined: Wed Oct 12, 2005 4:39 pm
Posts: 15
Location: India
Hi,
I am getting the NonUniqueObjectException. I am not able to see any reason why any other object would exist in memory. I have attached everything that I could think of as "relevant". I would be glad to provide any additional information you may need to solve this issue. I have spent almost a week trying to debug this error but it doesn't seem to go away! :(

Can any one out there throw some light on what I am doing wrong?

Thanks.
Best Regards,
Raghu


Hibernate version: 3.1


Mapping documents:

<hibernate-mapping>
<class name="patient.businessobject.PatientRegistrationDetail" table="patient_registration_detail">
<id name="id" column="Id">
<generator class="increment"/>
</id>
<property name="registrationDateTime"/>
<property name="firstName"/>
<property name="lastName"/>
<property name="middleName"/>
<property name="telNumber1"/>
<property name="telNumber2"/>
<property name="emailAddress"/>
<property name="sex"/>
<property name="dateOfBirthDate" column="DateOfBirth"/>
<property name="bloodGroup"/>
<property name="remarks"/>
<many-to-one name="registrationCentre" column="RegistrationCentreId" class="common.businessobject.Centre"/>
<set name="addresses" table="patient_address" lazy="true" cascade="all">
<key column="PatientId"/>
<composite-element class="patient.businessobject.PatientAddress">
<property name="type"/>
<property name="street1"/>
<property name="street2"/>
<property name="street3"/>
<property name="city"/>
<property name="state"/>
<property name="pincode"/>
</composite-element>
</set>
</class>

</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():

//this is a function in DataAccessManager class
public static void update(Object obj)
{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.update(obj);
tx.commit();
}

I use servlet filter to close the session.


The above function is called in a struts action class as follows:
DynaActionForm dynaForm = (DynaActionForm) form;
PatientRegistrationDetail prd = extractFromForm(dynaForm);
System.out.println("Updating PRD: "+prd);
DataAccessManager.update(prd);

The code is bombing when update function is called. I can't understand how there can be another object with the same id. Because this is the *start* of a new servlet request.


The code of extractFromForm is as follows:

private PatientRegistrationDetail extractFromForm(DynaActionForm dynaForm)
{
PatientRegistrationDetail prd = (PatientRegistrationDetail) dynaForm.get("patientRegistrationDetail");

PatientAddress contactAddress = (PatientAddress)dynaForm.get("contactAddress");
PatientAddress permanentAddress = (PatientAddress)dynaForm.get("permanentAddress");
Set addresses = new HashSet();
addresses.add(contactAddress);
addresses.add(permanentAddress);
prd.setAddresses(addresses);

return prd;
}



Full stack trace of any exception that occurs:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.lifeconnect.patient.businessobject.PatientRegistrationDetail#3]
org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:628)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:258)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:216)
org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:33)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:572)
org.hibernate.impl.SessionImpl.update(SessionImpl.java:561)
org.hibernate.impl.SessionImpl.update(SessionImpl.java:553)



Name and version of the database you are using:
MySQL 4.1.10



The generated SQL (show_sql=true):

Hibernate: select this_.Id as Id5_0_, this_.name as name5_0_, this_.remarks as remarks5_0_ from centre_master this_
Hibernate: delete from patient_address where PatientId=? and type=? and street1=? and street2=? and street3=? and city=? and state=? and pincode=?
Hibernate: insert into patient_address (PatientId, type, street1, street2, street3, city, state, pincode) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into patient_address (PatientId, type, street1, street2, street3, city, state, pincode) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select this_.Id as Id0_0_, this_.name as name0_0_, this_.password as password0_0_ from ums_user this_
Hibernate: delete from patient_address where PatientId=? and type=? and street1=? and street2=? and street3=? and city=? and state=? and pincode=?
Hibernate: insert into patient_address (PatientId, type, street1, street2, street3, city, state, pincode) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into patient_address (PatientId, type, street1, street2, street3, city, state, pincode) values (?, ?, ?, ?, ?, ?, ?, ?)



Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 12:46 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Two issues:

- Most importantly, use code tags when posting code here. I find that that solves almost all of my hibernate problems by default ;)

- Are you using a long-lived session? When is the session being created? If all the SQL you included is from the same session, then obviously several other objects are in the session already. If the object passed into your update statement is a detached version of one of those objects, then you'll get the error that you are seeing. To solve, either switch to proper, short-lived sessions, or change Session.update(obj) to Session.replicate(obj, ReplicationMode.OVERWRITE).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 2:07 am 
Newbie

Joined: Wed Oct 12, 2005 4:39 pm
Posts: 15
Location: India
Thank you for pointing it out that objects existed in the session when I was trying to do an update.

I prefer short transactions wherever possible. I accidentally commented out the servlet filter to close the connection at the end of each HTTP request, and hence objects retrieved in the previous request still existed in the Hibernate session. The problem is solved for me when I did ensure that the filter to close the connection was being applied properly.

Thanks for your time and response. :-)

Regards,
Raghu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.