-->
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.  [ 3 posts ] 
Author Message
 Post subject: Move object from one parent collection to another
PostPosted: Sun Jan 30, 2005 11:34 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 9:11 pm
Posts: 32
I'm using Hibernate 2.1.x with these mappings:
Code:
<class name="com..UserTitle" table="usr_title" lazy="true" discriminator-value="X">
      <id name="id" type="long" unsaved-value="0">
         <generator class="identity"/>
      </id>
      <discriminator column="type" type="char" length="1"/>
      <property name="assigned" type="date"/>
      <list name="userModules" lazy="true" cascade="all-delete-orphan">
         <key column="userTitle"/>
         <index column="sequence" type="int"/>
         <one-to-many class="com.UserModule"/>
      </list>
      <subclass name="com.CurrentTitle"  lazy="true" discriminator-value="C"/>     
      <subclass name="com.ArchiveTitle" lazy="true" discriminator-value="A"/>
   </class>
   <class name="com.UserModule" table="usr_module" lazy="true">
      <id name="id" type="long" unsaved-value="0">
         <generator class="identity"/>
      </id>
      <property name="lastAccess" type="timestamp"/>
      <property name="takenCount" type="int"/>
      <property name="recommended" type="boolean"/>
      <many-to-one name="userTitle" class="com.UserTitle"/>
   </class>


I am trying to move the UserModule(s) from a collection in CurrentTitle to a collection in ArchiveTitle. I then want to delete the CurrentTitle.

I am able to move the UserModule(s) from one parent to another. However, when I delete the CurrentTitle, the cascade='all-delete-orphan' throws an error since I have "moved" the UserModule associations.

Code:
CurrentTitle current = load(CurrentTitle.class, new Long(1));
ArchiveTitle archive = new ArchiveTitle(current);

delete(current);
save(archive);
commit(); //error on flush...


Code:
public class UserTitleArchive extends UserTitle{
   private List userModules = new ArrayList();
   public UserTitleArchive(){
      super();
   }
   public UserTitleArchive(CurrentTitle current){
      setAssigned(current.getAssigned());
      for(Iterator i = current.getUserModules().iterator(); i.hasNext();) {
         UserModule um = (UserModule) i.next();
         addModule(um);
      }
   }

   public void addModule(UserModule module){
      module.setUserTitle(this);
      getUserModules().add(module);
   }
}


The error message being generated is;
Code:
net.sf.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): 8, of class: com.itsolut.entity.course.UserModule
   at net.sf.hibernate.impl.SessionImpl.forceFlush(SessionImpl.java:734)
   at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:712)
   at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1347)


In order to do a move from one parent to another and delete the first parent:
- Do I have to create a "copy" of each UserModule to associate with the ArchiveTitle?
- is there another way? Maybe some way to break the original parents association with the child object?


Thanks in advance,
Chris.....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 04, 2005 10:35 am 
Newbie

Joined: Wed Apr 28, 2004 5:32 am
Posts: 12
You can not move a child to another parent when the option all-delete-orphan is set.

Use cascade="all" instead :

1) detach child from the first parent
2) update or delete first parent
3) attach child to his new parent
4) update new parent

You have to handle orphans by hand.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 04, 2005 12:41 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 9:11 pm
Posts: 32
Schmitt31 - thanks for the answer. Now that you point out that I should use the "all" cascade that makes perfect sense....

Chris....


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