-->
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.  [ 6 posts ] 
Author Message
 Post subject: cascade problem
PostPosted: Mon Dec 15, 2003 5:54 am 
Newbie

Joined: Thu Sep 25, 2003 9:25 am
Posts: 15
In my system I have users and one attribute for each:

Code:
<class name="A" table="attribute">
<id name="aId" unsaved-value="-1"><generator class="native"/></id>       
<property name="name" type="string" length="20" />      
...
</class>

<class name="User" table="users">
<id name="userId" unsaved-value="-1"><generator class="native"/></id>
<many-to-one name="a" class="A" column="aId" cascade="all-delete-orphan" />
...
</class>


Users <-> A relationship could well be one-to-one, but I'm using many-to-one just because it's easier to handle (the idea of one-to-one in Hibernate is quite confusing because in the version I'm using you cannot use foreign keys as reference and you have to have the same primary key value).

When I save new user (or update an existing one) I only need to handle the User object and cascade="all-delete-orphan" handles inserting a new value. But, for some users I'd like to set A to null (and then delete A from attribute table). If I try:
Code:
user.update(); // i need to update some other property values of user
user.getA().delete();


I get error:
Flush during cascade is dangerous - this might occur if an object was deleted and then re-saved by cascade, which is totally understandable I think.

Then, if I try:
Code:
user.setA(null);


the row in attribute table is not deleted although it's set to null in user table. Shouldn't "all-delete-orphans" to the deleting? Or, how can I set A in users table to null and delete the corresponding row from attribute table?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 7:34 am 
Newbie

Joined: Thu Sep 25, 2003 9:25 am
Posts: 15
Ok, seems that all-delete-orphan is not legal here :)

But, that gets me back to the original problem. When I try to delete attribute with user.getA().delete() I get error. How do I delete the attribute row?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 8:15 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
user.setA(null);
session.flush();
session.delete(a);

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 8:33 am 
Newbie

Joined: Thu Sep 25, 2003 9:25 am
Posts: 15
epbernard wrote:
Code:
user.setA(null);
session.flush();
session.delete(a);


Thanks. But doesn't that mean that the transaction is committed when the first flush is made? Now, what if deleting A fails? In that case changes to the user object should NOT be persisted.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 8:38 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Grrr, This is urban legend and FAQ ;-)
http://www.hibernate.org/74.html#A2

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 8:59 am 
Newbie

Joined: Thu Sep 25, 2003 9:25 am
Posts: 15
Ok, just testing whether you knew that! :) :)

Thanks!!


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