-->
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: Problems removing objects from a set
PostPosted: Tue Jan 25, 2005 1:21 am 
Beginner
Beginner

Joined: Sat Sep 25, 2004 12:42 am
Posts: 27
Note: I've moved this into a seperate thread as I think it's unrelated to this one.

I'm having some trouble removing objects from a persistent set (one-to-many). When I attempt to remove a child using the same instance of the set the child object was added to an ObjectDeletedException expection is thrown with "deleted object would be re-saved by cascade (remove deleted object from associations)".

Here is a simplified version of what I'm attempting to do:
Code:
// add the child to the parent
Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
c.setParent(p);
p.getChildren().add(c);
// NOTE: p is then placed into the HttpSession
session.save(c); // at this point the c.childId is assigned a value
session.flush();

// re-attach the parent that was placed in the session
session.lock(p, LockMode.NONE);
// using the same child id returned in the save code
Child c = (Child) session.load(Child.class, cid);
// IMPORTANT: the remove call below returns false, however, using the debugger I can see the instance of Child is there
p.getChildren().remove(c);  //
session.delete(c); // throws ObjectDeletedException: deleted object would be re-saved by cascade
session.flush();


I can however delete children that havn't been added in the current HttpSession...

The childId property is a Long, assigned using the seqhilo generator. Here are the equals and hashCode methods (although the equals method isn't called when removing the child):
Code:
    public boolean equals(Object o) {
        log.debug("equals called");
        if (this == o) return true;
        if (!(o instanceof Child)) return false;

        final Child child = (Child) o;

        if (childId != null ? !childId.equals(child.childId) : child.childId != null) return false;

        return true;
    }

    public int hashCode() {
        log.debug("hashCode called");
        return (childId != null ? childId.hashCode() : 0);
    }


I presume this doesn't work because when I added the Child to the parent the childId was null???

Anyone have any hints? What am I doing wrong?

Hibernate version:
Hibernate 2.1.7

Database
PostgreSQL 7.5

Mapping documents:
one-to-many mapping in Parent:
Code:
<set name="children" lazy="true" inverse="true" cascade="all" sort="unsorted">
    <key column="parent_id"> </key>
        <one-to-many class="Child"/>
</set>

many-to-one mapping in Child:
Code:
<many-to-one name="parent" class="Parent" cascade="save-update" outer-join="auto" update="true" insert="true" access="property" column="parent_id" not-null="true" unique="false"/>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 25, 2005 2:00 am 
Beginner
Beginner

Joined: Sat Sep 25, 2004 12:42 am
Posts: 27
I've done a little more mucking around and found that if I change the code to:
Code:
// add the child to the parent
Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
c.setParent(p);
// NOTE: p is then placed into the HttpSession
session.save(c); // at this point the c.childId is assigned a value
// IMPORTANT: it looks like the add works as expected if the Child being added has the correct childId
p.getChildren().add(c);
session.flush();


// re-attach the parent that was placed in the session
session.lock(p, LockMode.NONE);
// using the same child id returned in the save code
Child c = (Child) session.load(Child.class, cid);
p.getChildren().remove(c);
session.delete(c);
session.flush();


The change being the childId is known when the Child is added to the set of children. Given that net.sf.hibernate.collection.Set is just a wrapper for java.util.HashSet I presume there isn't a great deal that can be done about it. Still, it's probably worth updating [url]file:///d:/Development/Libraries/hibernate-2.1/doc/reference/html_single/index.html#parent-child-bidir[/url] with a gotchya... OR am I missing something???


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 25, 2005 2:25 am 
Beginner
Beginner

Joined: Sat Sep 25, 2004 12:42 am
Posts: 27
The URL in the previous post should be:
http://www.hibernate.org/hib_docs/refer ... hild-bidir

Also, the code in the previous post "fixes" my issue... I forgot to mention that. :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 25, 2005 9:17 am 
Beginner
Beginner

Joined: Sat Sep 25, 2004 12:42 am
Posts: 27
By some amazing genius I thought I might try removing the hashCode method. And, drum roll please... Everything works!

So simple, there I was conjuring up schemes to fix my problem and all I needed to do was remove the hashCode method.

:(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 25, 2005 9:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
See http://www.hibernate.org/109.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 25, 2005 7:31 pm 
Beginner
Beginner

Joined: Sat Sep 25, 2004 12:42 am
Posts: 27
Bugger, it looks like I do need to conjur up something afterall. Thanks for the info Michael.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 25, 2005 7:57 pm 
Beginner
Beginner

Joined: Sat Sep 25, 2004 12:42 am
Posts: 27
I've decided to keep the equals and hashCode methods and to evict and re-load the Parent after each write operation. Keeps things simple.

Cheers,
Dan


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.