-->
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.  [ 7 posts ] 
Author Message
 Post subject: Moving a child object to a different parent
PostPosted: Sun Nov 13, 2005 4:59 pm 
Newbie

Joined: Tue Sep 13, 2005 6:34 pm
Posts: 3
I am using hibernate 3 and I have a category object mapped with the hibernate mapping included at the bottom of this post

It has among others the following methods :


public void removeChildCategory(Category category){
if (category == null) {
throw new IllegalArgumentException("Category to be removed from category is null");
}
category.setParentCategory(null);
childCategories.remove(category);
}

public void addChildCategory(Category category){
if (category == null) {
throw new IllegalArgumentException("Category to be added to category is null");
}
category.setParentCategory(this);
childCategories.add(category);
}


The problem is that when i try to move one category to another

like this

Code:
booksCat.removeChildCategory(thrillersCat);
moviesCat.addChildCategory(thrillersCat);


I get the following exception

org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.zenggi.data.Category#379]

I am kind of at a loss as to why this is occuring since the parent field has cascade set to none and the item got removed from the first collection
can anyone offer some insight ?



Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.company">


<class name="Category"
      table="CATEGORY"
      lazy="true">

   <id name="id"
      type="long"
      column="ID"
      unsaved-value="null">
      <generator class="identity"/>
   </id>

    <version name="version" column="VERSION" type="integer"/>

    <property name="name" type="string">
        <column name="NAME" not-null="true"/>
    </property>

    <property name="description" type="string">
        <column name="DESCRIPTION" />
    </property>

    <many-to-one name="parentCategory"
                 class="Category"
                 lazy="false"
                 insert="false"
                 update="false"
                 cascade="none">
        <column name="PARENT_CATEGORY_ID"
                not-null="false" />
    </many-to-one>

    <list   name="childCategories"
            lazy="false"
            cascade="all-delete-orphan"
            inverse="false">
        <key column="PARENT_CATEGORY_ID"/>
        <list-index column="CATEGORY_INDEX" />
        <one-to-many class="Category"/>
    </list>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 13, 2005 5:06 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://forum.hibernate.org/viewtopic.php?t=936040


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 13, 2005 5:10 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://opensource2.atlassian.com/projec ... se/HHH-785


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 13, 2005 6:04 pm 
Newbie

Joined: Tue Sep 13, 2005 6:34 pm
Posts: 3
Christian thanks for your prompt response, could you perhaps elaborate a bit on the "complex session coding" you are refering to in the jira issue.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 13, 2005 7:04 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I did, in fact, there is a whole book about it and hundreds of pages of documentation. Read up on orphan-delete and delete operations.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 13, 2005 7:05 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The "complex" was for my particular use case, by the way. Yours is trivial. Don't use orphan-delete.


Top
 Profile  
 
 Post subject: Re: Moving a child object to a different parent
PostPosted: Mon Nov 14, 2005 6:04 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
jelmerk wrote:
...

Code:
booksCat.removeChildCategory(thrillersCat);
moviesCat.addChildCategory(thrillersCat);


I get the following exception

org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.zenggi.data.Category#379]

I am kind of at a loss as to why this is occuring since the parent field has cascade set to none and the item got removed from the first collection
can anyone offer some insight ?



Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.company">


<class name="Category"
      table="CATEGORY"
      lazy="true">

   <id name="id"
      type="long"
      column="ID"
      unsaved-value="null">
      <generator class="identity"/>
   </id>

    <version name="version" column="VERSION" type="integer"/>

    <property name="name" type="string">
        <column name="NAME" not-null="true"/>
    </property>

    <property name="description" type="string">
        <column name="DESCRIPTION" />
    </property>

    <many-to-one name="parentCategory"
                 class="Category"
                 lazy="false"
                 insert="false"
                 update="false"
                 cascade="none">
        <column name="PARENT_CATEGORY_ID"
                not-null="false" />
    </many-to-one>

    <list   name="childCategories"
            lazy="false"
            cascade="all-delete-orphan"
            inverse="false">
        <key column="PARENT_CATEGORY_ID"/>
        <list-index column="CATEGORY_INDEX" />
        <one-to-many class="Category"/>
    </list>

</class>

</hibernate-mapping>


I don't think that the problem comes from the mapping of the parent field.

I rather believe, it's the mapping of the list. There you have cascade="all-delete-orphan". AFAIK, this means that an object should be deleted, when it's removed from the collection.
On the other hand, you add the object to the collection of another parent where it is to be stored in the database.

If I understand Hibernate and your case right, you must not use "delete-orphan" in this case. AFAIK, you may only use it, if the objects within the collection really cease to exist, when they're removed from the collection.

_________________
hth,
Heinz
Don't forget to rate if this helped


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