-->
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: NonUniqueObjectException
PostPosted: Tue Dec 02, 2008 7:08 pm 
Newbie

Joined: Sun Nov 23, 2008 2:32 pm
Posts: 4
Ok I've got this error:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session.

I've done some searching on this error and seen plenty of results but nothing I've found has actually helped me get around this error.

So here's a bit of useful details. I have a web app (JSF) using hibernate as the data layer, then using spring beans to access it. I've set up a servlet filter that does something like

Code:
session.beginTransaction();

chain.doFilter(req,resp);

session.getTransaction().commit();

It's not exactly like that but you get the idea... I read that somewhere, don't remember where now but it seems to work well. The Spring beans handle all the interaction with hibernate, so other than the filter, the web app doesn't know about hibernate. In this particular instance (the error) I'm dealing with a persistent entity that has some relations (you probably guessed that already:) ). The mapping is something like...

Code:
<hibernate-mapping>
   <class name="com.company.db.beans.Kiosk" table="KIOSK_INFORMATION">
      <id name="id" column="id">
         <generator class="native" />
      </id>
      <property name="serialNumber" />
      <!-- other properties ... --->
      
      <many-to-one name="simCard" class="com.company.db.beans.SimCard"
         column="simCardId" unique="true" />

      <!-- some other similar relations -->
   </class>
</hibernate-mapping>

<!-- simcard -->

<hibernate-mapping>
   <class name="com.iceskill.db.beans.SimCard" table="SIM_CARD">
      <id name="id" column="id">
         <generator class="native" />
      </id>
      <property name="cardTelNumber" />
      
      <one-to-one name="kiosk" property-ref="simCard" class="com.company.db.beans.Kiosk" cascade="none"/>
   </class>
</hibernate-mapping>



I have a saveKiosk method which does ...
Code:
Session session = getSession(); // <-- that method gets the current session
session.saveOrUpdate(kiosk);


And it fails on saveOrUpdate... but not always. I can create a Kiosk object, save it, modify and save it, over and over again just fine. The problem only arises when I add a SimCard object. The first time it works, then if I open the edit page, and hit save (without even changing the value of the simcard, or any value for that matter, I get the NonUniqueObjectException.

I've tried using session.merge(). This works but causes other problems later. I don't want to get into that since this post is already long.

I also tried doing session.evict(kiosk) right before the saveOrUpdate call, no help there.

I read somewhere else putting cascade="none" for the relations may help. No help for me.

What I don't get is, this is two completely different requests, and I'm assuming two different session, I'm not sure how hibernate's internal session management works. But on the second request I'm not loading the object again.

Anyway, I would really appreciate any help on this matter.
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 3:32 pm 
Newbie

Joined: Sun Nov 23, 2008 2:32 pm
Posts: 4
Well I've been able to sort of resolve this problem, but the solution basically involves using merge when it's an existing object and save for a new object. I still think saveOrUpdate should work. Now I have to have 2 different save methods. Anyway... I'm still open to suggestions but if there's no other way I guess this will suffice.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 5:15 pm 
Newbie

Joined: Thu Nov 27, 2008 2:43 pm
Posts: 3
After the way it sound, seems you try to put twice the same object. Try session.flush()

I don't know what exactly are you doing there but i give an example where this exception appears randomly:

Suppose you have a DAO with a getAll() method which returns a criteria.list().
Let's say you want to put two objects into another collecion this way:

dao.start();
collection = dao.getAll();
for (DataType iter : collection) {
col.add(iter);
break;
}
//dao.flush();
collection = dao.getAll();
for (DataType iter : collection) {
col.add(iter);
}
dao.stop();

Sometimes it works. Sometime it fails. To be sure that it works, flush between operations. The reason is the the collection is not the same and it is possible that in two different operations over two "different" collections the accessed object to be the same.


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.