-->
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: delete-orphan not behaving like the all-delete-orphan???
PostPosted: Thu Oct 21, 2004 1:30 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
All I am doing is clearing out the collection. I happen to have a table of 2 items. I get the 1st item whose primary key is 1(I added it to the db myself). With the option of delete-orphan, the orphan is not deleted even though I removed the reference from the collection. Once I switch to all-delete-orphan, the below code starts working. Is this a bug?

The table is just a table representing a tree
CAT_ID(PK), name VARCHAR(100), PARENT_CAT_ID(FK to this tables CAT_ID)
thanks for any assistance here,
dean


Hibernate version:
hibernate 2.1
Mapping documents:
Code:
        <set
            name="childCategories"
            table="CHAP4_CATEGORY"
            lazy="false"
            inverse="true"
            cascade="delete-orphan"
            sort="unsorted"
        >

              <key
                  column="PARENT_CAT_ID"
              >
              </key>

              <one-to-many
                  class="biz.xsoftware.model.chap4.Category"
              />
        </set>

        <many-to-one
            name="parentCategory"
            class="biz.xsoftware.model.chap4.Category"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            access="property"
            column="PARENT_CAT_ID"
        />

Code between sessionFactory.openSession() and session.close():
Code:
Category computer = (Category)session.load(Category.class, new Long(1));
         
         Set s = computer.getChildCategories();
         System.out.println("size="+s.size());
         s.clear();
         System.out.println("size="+s.size());

Full stack trace of any exception that occurs:

Name and version of the database you are using:
MySQL
The generated SQL (show_sql=true):
Code:
Hibernate: select category0_.CAT_ID as CAT_ID1_, category0_.NAME as NAME1_, category0_.PARENT_CAT_ID as PARENT_C3_1_, category1_.CAT_ID as CAT_ID0_, category1_.NAME as NAME0_, category1_.PARENT_CAT_ID as PARENT_C3_0_ from CHAP4_CATEGORY category0_ left outer join CHAP4_CATEGORY category1_ on category0_.PARENT_CAT_ID=category1_.CAT_ID where category0_.CAT_ID=?
Hibernate: select childcateg0_.PARENT_CAT_ID as PARENT_C3___, childcateg0_.CAT_ID as CAT_ID__, childcateg0_.CAT_ID as CAT_ID0_, childcateg0_.NAME as NAME0_, childcateg0_.PARENT_CAT_ID as PARENT_C3_0_ from CHAP4_CATEGORY childcateg0_ where childcateg0_.PARENT_CAT_ID=?
Hibernate: select childcateg0_.PARENT_CAT_ID as PARENT_C3___, childcateg0_.CAT_ID as CAT_ID__, childcateg0_.CAT_ID as CAT_ID0_, childcateg0_.NAME as NAME0_, childcateg0_.PARENT_CAT_ID as PARENT_C3_0_ from CHAP4_CATEGORY childcateg0_ where childcateg0_.PARENT_CAT_ID=?
size=1
size=0

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 21, 2004 1:48 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
take a look at inverse semantics.
we also don't know if your association is bidirectionnal or not

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 21, 2004 10:25 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
I am not sure what inverse would have to do with that. Does inverse behave differently for all-delete-orphan than it does for delete-orphan? If not, I don't expect the behavior to change.

I am posting the entire xml mapping file, though I thought the snippet I had pasted eluded to the bidirectional relationship??? I am new though to hibernate so maybe I am wrong. Anyways, the entire mapping file is below...(NOTE: I keep flipping the cascade option between all-delete-orphan and delete-orphan)

Code:
<hibernate-mapping>
    <class
        name="biz.xsoftware.model.chap4.Category"
        table="CHAP4_CATEGORY"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="CAT_ID"
            type="java.lang.Long"
        >
            <generator class="increment">
            </generator>
        </id>

        <set
            name="childCategories"
            table="CHAP4_CATEGORY"
            lazy="false"
            inverse="true"
            cascade="all-delete-orphan"
            sort="unsorted"
        >

              <key
                  column="PARENT_CAT_ID"
              >
              </key>

              <one-to-many
                  class="biz.xsoftware.model.chap4.Category"
              />
        </set>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="NAME"
        />

        <many-to-one
            name="parentCategory"
            class="biz.xsoftware.model.chap4.Category"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            access="property"
            column="PARENT_CAT_ID"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Category.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 21, 2004 11:50 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
inverse = true means that the association is not managed by working with the collection defined with one-to-many but by the object defined in the many-to-one on the other side...

this is said in many docs, reference guide, hibernate in action, wiki, many topics

if you want to "delete" (sql meaning) you have to call child.setParent(null)

generally you use addChild & removeChild methods in the parent, these method manage both sides

example
in Parent.java
Code:
   public void removeLigneChild(Child child) {
      if (child==null)
         throw new IllegalArgumentException(" ");
      
      child.setParent(null);
      this.getChildren().remove(child);

   }


You'll find a lot of best practices like this in hibernate in action

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 21, 2004 12:31 pm 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
funny you mention it. I am doing this out of the "hibernate in action" book. I do know about the thing you mention already.

That was a very good point. I did such a thing on the add and forgot to do it on the remove but I am confused on one last minor detail.

The only change I made was between all-delete-orphan to delete-orphan and I am trying to understand why the behavior changed when I don't have the code that I should and will add(ie. just looking for understanding here as I read the book)

I understand now I think why delete-orphan does not work. The orphan child still has a reference to it's parent, so it is not really an orphan, more like a half orphan as it is not in the set...ie. The bidirectional relationship was not cleaned up properly by me.

Then my question becomes why in the world is all-delete-orphan working...The only difference here is it adds save-updates to be cascaded also. I haven't changed the child object, just removed it from the collection. How is it that all-delete-orphan is behaving differently then delete-orphan? Bottom line...I expected the same behavior which as you point out should not be to erase it from the database as I did not clean up the bidirectional relationship properly.

thanks,
dean


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 21, 2004 11:19 pm 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
I went home and tried that, and it did not fix the problem.

So basically delete-orphan appears to not be working. Has anybody else tried to use delete-orphan yet????

The only difference now, is that instead of deleting the orphan, the orphan's ref to the parent is set to null.

ie. I had a table
1, computers, null
2, laptop, 1

and removing laptop from the computers collection as well as nulling out the parent reference that laptop had to computer with cascade="delete-orphan" results in

1, computers, null
2, laptop, null

Am I using this incorrectly?
thanks,
dean


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 23, 2004 10:21 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
is there no one who knows this one???


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.