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.....