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.  [ 8 posts ] 
Author Message
 Post subject: cascade="all-delete-orphan"
PostPosted: Fri Sep 12, 2008 6:12 am 
Beginner
Beginner

Joined: Thu Jul 31, 2008 8:28 pm
Posts: 24
NHibernate version: 2.0.0.3001

In NHibernate docs http://nhforge.org/doc/nh/en/#example-parentchild just before section 18.4 there is the following line:
Now, in our case, a Child can't really exist without its parent. So if we remove a Child from the collection, we really do want it to be deleted. For this, we must use cascade="all-delete-orphan".

I have tried to use this mapping of all-delete-orphan but I keep on getting NULL VIOLATION exceptions.
As I understand this happen due to the fact that removing a child entity from a parent entities' collection only remove the link between them, not removing the child entity.
But the whole point of all-delete-orphan is to remove the orphan child, doesn't it?

Please help me figure this out.

Ido.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2008 7:48 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I admit that the doc is a bit unprecise with

Quote:
"So if we remove a Child from the collection, we really do want it to be deleted. For this, we must use cascade="all-delete-orphan"."

My understanding is this: Cascade is only used if you do something with the parent (Save/Update/Delete). Every action you do on the parent ist cascaded to the children. And in the special case of "all-delete-orphan" the children will also be deleted if you delete the parent (not only the association). If you just remove a child from the parents collection you have to delete the child by yourself. Cascading is not an option here.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: cascade="all-delete-orphan"
PostPosted: Fri Sep 12, 2008 10:05 am 
Newbie

Joined: Fri Sep 12, 2008 10:00 am
Posts: 1
Just curious, so what does "all-delete-orphan" do?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2008 10:13 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
In my understanding:

session.Delete(parent);

results in:

- delete of parent (delete)

if "all-delete-orphan":
- delete children (delete)

else
- delete of association to children (update)

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: delete-orphan
PostPosted: Fri Sep 12, 2008 11:15 am 
Beginner
Beginner

Joined: Thu Jul 31, 2008 8:28 pm
Posts: 24
I figure this out!
NHibernate works almost great. The problem is that NHibernate use two separate SQL statements: One to remove the link between the parent and child entities and another to remove the orphan child.
My database schema did not allow null in the parent column, when I change it to allow null in the parent column of the child table everything works great.
The only problem is that the database will not protect me in this way.

Ido


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 14, 2008 6:28 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
OK, then my explanation was obviously wrong. But at least the functionality is as expected ;-).

If you want to change the insert+update behavior, you have to map the collection as inverse and add a many-to-one association to the items. The hibernate should do a single insert for a new child and a single delete for a removed child. But you have to take care of a lot of other things then. Have a look at

http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/example-parentchild.html

and

http://simoes.org/docs/hibernate-2.1/155.html

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: About "Inside explanation of inverse=true"
PostPosted: Sun Sep 14, 2008 8:24 am 
Beginner
Beginner

Joined: Thu Jul 31, 2008 8:28 pm
Posts: 24
Hi,
I have read the document you put in your last post (http://simoes.org/docs/hibernate-2.1/155.html).
The thing is they only describe insert and update, not delete :)
I can't understand who wrote it and how to write him/her. I hope you know who it is so we can ask him/her to complete the document with delete sections.

Thanks for the answers!

Ido.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 14, 2008 8:31 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Nope, I have no clue. But that is "hibernate" stuff anyway. But would be nice to have something like that on http://www.nhforge.org, too.

I got the link from this thread, maybe Dario can help.

http://groups.google.com/group/nhusers/browse_thread/thread/4541701c9367e642

_________________
--Wolfgang


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