-->
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: de-reference before deletion=NonUniqueObjectException
PostPosted: Thu Apr 10, 2008 7:12 pm 
Beginner
Beginner

Joined: Thu Jul 22, 2004 10:51 pm
Posts: 29
Location: sydney australia
Hi all,

I have a parent/child relationship and when the parent is deleted, i need to remove the references to the parent from all of it's children and keep them and then delete the parent. I'd like this to happen in one transaction so that if the final delete fails, the references are kept. I'm having trouble, hibernate is complaining that the parent is already referenced in the session. I'm assuming this is because it's attached to the session when i use it as a parameter for the children query, or when the children return with references to the parent. I've tried using evict and merge methods.

any tips appreciated.

Thx, paul.

Hibernate version: 3.2.3ga

Mapping documents:
Code:
<hibernate-mapping>
   <class name="com.Parent">
      <id name="id" column="pk"><generator class="native"/></id>
   </class>
</hibernate-mapping>
Code:
<hibernate-mapping>
   <class name="com.Child">
      <id name="id" column="pk"><generator class="native"/></id>
      <many-to-one name="parent" class="com.Parent" lazy="false"/>
   </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
// Parent parent (argument)
Session session = sFac.getSession();
try
{
   session.beginTransaction();

   Query qry = session.createQuery("FROM " + Child.class.getName() + " AS child WHERE child.parent=:parent");
   qry.setEntity("parent", parent);

   for (Child child : (List<Child>) qry.list())
   {
      child.setParent(null);
      session.saveOrUpdate(child);
   }

   session.delete(parent); // <-- exception thrown here
   session.getTransaction().commit();
}
catch (RuntimeException e)
{
   rollback();
   throw e;
}


Full stack trace of any exception that occurs:
Code:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.Parent#30]
   at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:578)
   at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:99)
   at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:52)
   at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:766)
   at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:744)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
   at $Proxy13.delete(Unknown Source)


Name and version of the database you are using: HSQLDB


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 13, 2008 8:39 pm 
Beginner
Beginner

Joined: Thu Jul 22, 2004 10:51 pm
Posts: 29
Location: sydney australia
I've solved it,

adding a merge of the Parent just before delete:

Code:
parent = (Parent) session.merge(parent);
session.delete(parent);


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.