-->
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: list cascade="all-delete-orphan" does not work!
PostPosted: Tue Jul 06, 2004 11:06 am 
Newbie

Joined: Tue Jul 06, 2004 10:14 am
Posts: 2
Hi,

I have to classes VendorDateEntry and UserDateEntry that both extend
a class DateEntry. I have a class EntryContainer that has a mixed list of VendorDateEntries and UserDateEntries. I want to use a table-per-concrete class strategy for VendorDateEntry and UserDateEntry.

After a lot of trial an error I finally found a mapping that works, i.e. the database tables look the way I expect them to look and the retreived objects are correct. The problem I have now, is that cascade="all-delete-orphan" does not work, i.e. when I delete a DateEntry from the list in EntryContainer, the corresponding table row is still there, although it is not contained in any list.

All suggestions are welcome.
All help is appreciated.

Here are my data:

hibernate version: 2.1.4
DB version: MySQL 4.1.1a


<hibernate-mapping>
<class
name="de.oew.test.EntryContainer"
table="test_entry_container"
dynamic-update="false"
dynamic-insert="false"
>

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

<list
name="dateEntryList"
table="test_entry_list"
lazy="false"
inverse="false"
cascade="all-delete-orphan"
outer-join="auto"
>

<key
column="container_id"
/>

<index
column="entry_position"
/>

<many-to-any
meta-type="class"
id-type="long">
<!-- VendorDateEntry, UserDateEntry -->
<column name="entry_type"/>
<column name="entry_id"/>
</many-to-any>
</list>

</class>

</hibernate-mapping>

<hibernate-mapping>
<class
name="de.oew.test.UserDateEntry"
table="test_user_date_entry"
dynamic-update="false"
dynamic-insert="false"
>

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

<property
name="date"
type="date"
update="true"
insert="true"
column="date"
/>

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

</class>

</hibernate-mapping>

<hibernate-mapping>
<class
name="de.oew.test.VendorDateEntry"
table="test_vendor_date_entry"
dynamic-update="false"
dynamic-insert="false"
>

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

<property
name="date"
type="date"
update="true"
insert="true"
column="date"
/>

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

</class>

</hibernate-mapping>


Java code:

private static void saveOrUpdate(Object object) throws HibernateException{
try{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(object);
tx.commit();
}
catch(HibernateException exc){
exc.printStackTrace();
}
finally{
HibernateUtil.closeSession();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2004 11:57 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Are you using the same original collection (ie don't use parent.setChilds(new Collection());
It is forbidden for a all-delete-orphan use

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Here is my Tester class!
PostPosted: Tue Jul 06, 2004 12:07 pm 
Newbie

Joined: Tue Jul 06, 2004 10:14 am
Posts: 2
Thanks for the reply! I use the List returned from a session.find(). But here is my tester class, so you can see what exactly I am doing.


package de.oew.test;

import java.util.List;
import java.util.Date;
import java.util.Iterator;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

import de.oew.hibernate.HibernateUtil;

public class MappingTester{

private static void saveOrUpdate(Object object) throws HibernateException{
try{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(object);
tx.commit();
}
catch(HibernateException exc){
exc.printStackTrace();
}
finally{
HibernateUtil.closeSession();
}
}

private static List retrieveFromDB() throws HibernateException{
List list = null;

try{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();

list = session.find("from " + EntryContainer.class.getName());
tx.commit();
}
catch(HibernateException exc){
exc.printStackTrace();
}
finally{
HibernateUtil.closeSession();
}
return list;
}

private static void fillDB(){
try{
EntryContainer container = new EntryContainer();

UserDateEntry ude1 = new UserDateEntry(new Date(), "Entry 1");
UserDateEntry ude2 = new UserDateEntry(new Date(), "Entry 2");
VendorDateEntry vde1 = new VendorDateEntry(new Date(), "Entry 3");
VendorDateEntry vde2 = new VendorDateEntry(new Date(), "Entry 4");

container.addDateEntry(ude1);
container.addDateEntry(ude2);
container.addDateEntry(vde1);
container.addDateEntry(vde2);

saveOrUpdate(container);
}
catch(HibernateException exc){
exc.printStackTrace();
}
}

private static void printDBObjects(){
try{
for(Iterator iter=retrieveFromDB().iterator(); iter.hasNext(); ){
EntryContainer container = (EntryContainer)iter.next();
System.out.println("<EntryContainer id=\""+container.getId()+"\">");
int i=0;
for(Iterator jter=container.getDateEntryList().iterator(); jter.hasNext(); i++){
DateEntry entry = (DateEntry)jter.next();
System.out.println(" <entry position=\""+i+"\" text=\""+entry.getText()+"\" class=\""+entry.getClass().getName()+"\"/>");
}
} System.out.println("</EntryContainer>");
}
catch(HibernateException exc){
exc.printStackTrace();
}
}

private static void changeContainer(){
try{
for(Iterator iter = retrieveFromDB().iterator(); iter.hasNext();){
EntryContainer ec = (EntryContainer)iter.next();
List list = ec.getDateEntryList();
DateEntry entry = (DateEntry)list.get(2);
list.remove(entry);
list.add(0, entry);
if(0 < list.size()){
list.remove(list.size() - 1); // remove last if exist
}
saveOrUpdate(ec);
}
}
catch(HibernateException exc){
exc.printStackTrace();
}
}

public static void main(String[] args){
fillDB();
printDBObjects();
changeContainer();
printDBObjects();
}
}


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.