-->
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.  [ 4 posts ] 
Author Message
 Post subject: Parent Child relationships
PostPosted: Wed Jul 09, 2008 1:58 pm 
Newbie

Joined: Wed Jul 09, 2008 1:50 pm
Posts: 2
[b]Hibernate version:3.2.6.ga[/b]

[b]Hibernate annotations version:3.3.1.GA[/b]


I have an Alert class which has a reference to a Collection of Comment objects. Using annotations "OneToMany" and the cascade=CascadeType.ALL, I can delete an Alert and it will delete the associated Comment objects. However, is there a way to delete the Comment and have it update the Collection of Comment objects of the referenced Alert. Right now it causes a foreign key exception.

Any ideas?

Roshan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 3:48 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Here is an example which works for me when I do an update call on the owner of the collection (all my objects are detached before update call):

Code:
@OneToMany(mappedBy ="assessment", cascade = {CascadeType.ALL}, fetch=FetchType.EAGER)
  @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  private Set<FinancePosition>financePositions = new HashSet<FinancePosition>();


There must be some other problem in your code or mapping, when you get a foreign key exception.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 8:44 pm 
Newbie

Joined: Wed Jul 09, 2008 1:50 pm
Posts: 2
I dont believe that works if you try to delete the child itself. Which is what I am having issues with? For example, Parent has multiple Children, then by deleting a child, without reference to the parent, the parent's join table/collection should be updated as well. I feel as if the delete should first remove the child from the Join table and then delete the child itself.

Parent <>--- Child

//with no access to the parent
session.delete(child)

Roshan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 2:09 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
In my own stuff I just do remove the child from the parent's collectionand the I call the merge method on the parent.

The following is from the Hibernate Annotations Doc, p.49:

Quote:
DELETE_
ORPHAN applies only to @OneToMany associations, and indicates that the delete()/remove() operation
should be applied to any child object that is removed from the association. In other words, if a child is dereferenced
by a persistent parent and if DELETE_ORPHAN is used, the "orphaned" child is deleted.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


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