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