-->
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.  [ 5 posts ] 
Author Message
 Post subject: deleted entity passed to persist
PostPosted: Thu Mar 19, 2009 5:36 am 
Newbie

Joined: Thu Mar 19, 2009 4:59 am
Posts: 9
Hello

I have a gui tree and want to handle Drag&Drop handling in one transaction.
Drag an TreeNode on to another TreeNode should do the following on the server; remove the dragging TreeNode from it's parent and add it to the new parent.
Well this always results in the same exception:
org.hibernate.ObjectDeletedException: deleted entity passed to persist:

Here my code:
Code:
         EntityManager em = getEntityManager();
         em.getTransaction().begin();

         parent.getChilds().remove(child);
         target.getChilds().add(child);
         em.persist(parent);
         em.persist(target);
         em.persist(child);

         em.getTransaction().commit();
         em.close();


Is this not possible in one transaction?

Appreciate any hint/help.
flavio


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 8:24 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
What are the cascades which you have specified in mapping?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 8:52 am 
Newbie

Joined: Thu Mar 19, 2009 4:59 am
Posts: 9
I'm using EMF and Teneo to persist the EMF objects.
Teneo generates the following mapping file:

Code:
   <subclass name="com.softmodeler.model.impl.TreeNodeImpl" entity-name="TreeNode" abstract="false" lazy="false" extends="BasicObject" discriminator-value="TreeNode">
      <meta attribute="eclassName">TreeNode</meta>
      <meta attribute="epackage">http:///www.softmodeler.com/model/softmodeler/1.0.0</meta>
      <property name="name" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String">
         <column not-null="false" unique="false" name="`name`"/>
      </property>
      <list name="childs" lazy="true" cascade="all,delete-orphan">
         <key update="true">
            <column name="`treenodechild_parent_id`" not-null="false" unique="false"/>
         </key>
         <list-index column="`treenode_childs_idx`"/>
         <one-to-many entity-name="TreeNodeChild"/>
      </list>
      <many-to-one name="object" entity-name="ObjectRef" lazy="false" cascade="all" foreign-key="treenode_object" insert="true" update="true" not-null="false">
         <column not-null="false" unique="false" name="`objectref_object_id`"/>
      </many-to-one>
      <property name="type" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String">
         <column not-null="false" unique="false" name="`type`"/>
      </property>
   </subclass>
   <class name="com.softmodeler.model.impl.TreeNodeChildImpl" entity-name="TreeNodeChild" abstract="false" lazy="false" discriminator-value="TreeNodeChild" table="`treenodechild`">
      <meta attribute="eclassName">TreeNodeChild</meta>
      <meta attribute="epackage">http:///www.softmodeler.com/model/softmodeler/1.0.0</meta>
      <id name="nodeId" type="java.lang.String">
         <column not-null="true" unique="false" name="`nodeid`"/>
      </id>
      <discriminator column="`dtype`" type="string"/>
      <version name="e_version" column="e_version" access="org.eclipse.emf.teneo.hibernate.mapping.property.VersionPropertyHandler">
         <meta attribute="syntheticVersion">true</meta>
      </version>
      <many-to-one name="parent" entity-name="TreeNode" lazy="false" foreign-key="treenodechild_parent" insert="false" update="false" not-null="false">
         <column not-null="false" unique="false" name="`treenodechild_parent_id`"/>
      </many-to-one>
   </class>


In the above code snippet parent and target are TreeNode instances and child is a TreeNodeChild instance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 9:10 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Its happening coz of the delete-orphan cascade which is there in:
Quote:
<list name="childs" lazy="true" cascade="all,delete-orphan">
<key update="true">
<column name="`treenodechild_parent_id`" not-null="false" unique="false"/>
</key>
<list-index column="`treenode_childs_idx`"/>
<one-to-many entity-name="TreeNodeChild"/>
</list>

So when you are doing parent.getChilds().remove(child); hibernate will delete the child entity. And in the next statement you are trying to add this deleted entity to another TreeNode.

Now you have 3 solutions:
    1. Remove insert=false update=false from the many-to-one side and do child.setParent(target);
    2. Remove the "delete-orphan" from the cascade.
    3. Clone the child object before parent.getChilds().remove(child) and then instead of target.getChilds().add(child); do target.getChilds().add(clonedChild); So it means when you are moving a child from one node to another, you are actually deleting the original child and adding a new child of same properties (except for id) as original to the target.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 11:11 am 
Newbie

Joined: Thu Mar 19, 2009 4:59 am
Posts: 9
It worked, thanks a lot!

I used solution 2 and removed the 'delete-orphan'.

For anybody who is also using Teneo here the 'childs' attribute in the ecore file:
Code:
    <eStructuralFeatures xsi:type="ecore:EReference" name="childs" upperBound="-1"
        eType="#//TreeNodeChild" containment="true" eOpposite="#//TreeNodeChild/parent"
        eKeys="#//TreeNodeChild/nodeId">
      <eAnnotations source="teneo.jpa">
        <details key="appinfo" value="@OneToMany(cascade={MERGE,PERSIST,REFRESH})"/>
      </eAnnotations>
    </eStructuralFeatures>


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