-->
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.  [ 2 posts ] 
Author Message
 Post subject: all-delete-orphan
PostPosted: Tue May 03, 2005 4:02 pm 
Newbie

Joined: Fri Apr 29, 2005 2:35 pm
Posts: 7
Hi everyone.

I have a master-detail cadastre (like Employees and their dependents).
My master class is called SuperClasse and my Detail class is called ClasseDetalhe.

So what is happening is that when I am going to update my master class (SuperClasse), i receive this exception message:

net.sf.hibernate.HibernateException: You may not dereference a collection with cascade="all-delete-orphan"

What I am doing to execute this update is that:

- I have a SuperClasse class whit properties updated (but not persistent yet), so I load a original class (a persistent class with the old values) and I copy all values from my new SuperClasse class to my original and persistent SuperClasse class, and commit that.

So, if I have cascade="save-update", it works, but if my new SuperClasse class have a Collection whit not every ClasseDetalhe classes, hibernate puts NULL on ClasseDetalhe ForeignKey for SuperClasse, and what I want it does was delete those ClasseDetalhes that it puts NULL, so I choice cascade="all-delete-orphan".

My hbm.xml's

SuperClasse.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 1.1//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="br.com.softsite.sample.model.SuperClasse"
table="SUPER_CLASSE"
>

<id
name="id"
column="ID"
type="java.lang.Integer"
>
<generator class="hilo">
</generator>
</id>

<property
name="descricao"
type="java.lang.String"
>
<column
name="DESCRICAO"
unique="false"
sql-type="VARCHAR(255)"
/>
</property>

<set
name="theClasseDetalhe"
lazy="true"
inverse="false"
cascade="all-delete-orphan"
sort="unsorted"
>

<key
column="THE_SUPER_CLASSE_FK"
/>

<one-to-many
class="br.com.softsite.sample.model.ClasseDetalhe"
/>
</set>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-SuperClasse.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>


ClasseDetalhe.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 1.1//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="br.com.softsite.sample.model.ClasseDetalhe"
table="CLASSE_DETALHE"
>

<id
name="id"
column="ID"
type="java.lang.Integer"
>
<generator class="hilo">
</generator>
</id>

<property
name="descricao"
type="java.lang.String"
>
<column
name="DESCRICAO"
unique="false"
sql-type="VARCHAR(255)"
/>
</property>

<many-to-one
name="theSuperClasse"
class="br.com.softsite.sample.model.SuperClasse"
cascade="none"
outer-join="auto"
column="THE_SUPER_CLASSE_FK"
unique="false"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-ClasseDetalhe.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: all-delete-orphan
PostPosted: Tue May 03, 2005 4:10 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
I've seen this exception if you read in an object which contains an associated collection... and then update it using something like
Code:
Set mySet = new HashSet();
mySet.add(...);
myPersistedObject.setMySet(mySet);


This is because when you load a hibernate associated collections, it uses it's own implmentation of Set so that it can keep track of adds and deletes.

It recommends using something like
Code:
myPersistedObject.getMySet().clear();
myPersistedObject.getMySet().add(....);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.