-->
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.  [ 8 posts ] 
Author Message
 Post subject: delete old child set before insert new child set
PostPosted: Wed Sep 01, 2004 10:57 pm 
Beginner
Beginner

Joined: Thu Jul 22, 2004 4:32 am
Posts: 31
parentPO.setChildSet(childSet);
I want to delete old child set from database before insert new child set

What should I do?

ParentPO.hbm.xml
Code:
        <set name="childSet" lazy="true" inverse="true" cascade="all">
            <key column="parentId"/>
            <one-to-many class="ParentPO"/>
        </set>


ChildPO.hbm.xml
Code:
        <many-to-one name="parentPO" class="ParentPO">
                 <column name="parentId" />
        </many-to-one>


Any suggestion would be appriciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 01, 2004 11:15 pm 
Beginner
Beginner

Joined: Thu Jul 22, 2004 4:32 am
Posts: 31
When I set ParentPO.hbm.xml like this:
<set name="userSet" lazy="true" inverse="true" cascade="all-delete-orphan">
a error occurs:
net.sf.hibernate.HibernateException: You may not dereference a collection with cascade="all-delete-orphan"
at net.sf.hibernate.impl.SessionImpl.updateUnreachableCollection(SessionImpl.java:2915)
at net.sf.hibernate.impl.SessionImpl.flushCollections(SessionImpl.java:2784)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2257)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2235)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 2:30 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
My guess is, that your replacing the Set. Instead clear it and then fill the new entries into the empty one.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 5:30 am 
Beginner
Beginner

Joined: Thu Jul 22, 2004 4:32 am
Posts: 31
ernst_pluess,Thanks for you reply.
But this still did not work.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 6:11 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Please show the Java code between sessionFactory.openSession() and session.close().
The red box! you know?

Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 6:18 am 
Beginner
Beginner

Joined: Thu Jul 22, 2004 4:32 am
Posts: 31
parentPO = session.load(ParentPO.class,"p001");
childPO1.setParentPO(parentPO);
childPO2.setParentPO(parentPO);
HashSet set = new HashSet();
set.add(childPO1);
set.add(childPO2);

parentPO.setChildSet(set);


I want hibernate to delete old child set before insert,but it doesn't work correctly,It will insert two child po into database directly


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 7:35 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you should have 2 methods (addChild() & removeChild()) to have a secure object graph when working on your collection.

Anyway, change

Code:
HashSet set = new HashSet();
set.add(childPO1);
set.add(childPO2);

parentPO.setChildSet(set);


to

Code:
parentPO.getChildSet().add(childPO1);
parentPO.getChildSet().add(childPO2);


oh, and if you want to remove all childs, just call parentPO.getChildSet().removeAll(); (but don't forget, for each child, to call theChild.setParent(null).

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 9:44 pm 
Beginner
Beginner

Joined: Thu Jul 22, 2004 4:32 am
Posts: 31
Thanks a million.


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