-->
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.  [ 7 posts ] 
Author Message
 Post subject: Fail to cascade delete child object from parent
PostPosted: Wed Jul 09, 2008 4:43 pm 
Newbie

Joined: Tue May 27, 2008 3:46 pm
Posts: 5
I have a problem when trying to delete a child object from a parent object which has a collection of this child object. I've done a lots of googling and have tried many combination of solutions, it still doesn't work. I am really confused at this point. Please help me!

For example:

For the Parent "Collection" object, I have

Code:
@OneToMany(cascade=CascadeType.ALL, mappedBy="collection", fetch=FetchType.EAGER)
@Cascade(value={org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
public Set<Book> getBooks() {
      return books;
   }


For the Child "Book" object, I have
Code:
@ManyToOne
@OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
@JoinColumn(name="COLLECTION_ID", nullable=false, insertable=true, updatable=true)
public Collection getCollection() {
      return collection;
}


I am using a Flex UI client front end talking to Java backend. All of the following JUnit test (w/o running Flex) are fine (in terms of cascading deleting and adding):
(1) remove Collection
(2) add Book into Collection
(3) remove Book from Collection

The problem appears when I try to remove a Book from Collection object when running Flex UI client frontend, through calling Hibernate save(Collection collection), (3) fails with no exception being thrown; the book object supposed to get deleted still alive in the database!! But (1) and (2) still work fine.

Your enlightenment is highly appreciated!!

baffled hibernate newbie....

Hibernate version:3.2.5.ga
Name and version of the database you are using:MySQL 5


Last edited by alimaned on Fri Jul 11, 2008 12:41 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 9:58 am 
Newbie

Joined: Tue May 27, 2008 3:46 pm
Posts: 5
anybody cares venturing a suggestion?

I tried to use MERGE instead, still not working.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 5:09 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
well, I don't know if the slight differences from my working example make the big differences. Anyway, here it is:

Code:
// 1)
// the owner (parent) of the bankAccounts is a Contact

@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
   @JoinColumn(name = "CONTACT_ID", nullable = false)
   @org.hibernate.annotations.IndexColumn(name="BANK_LISTPOS")
   @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private List<BankAccount> bankAccounts = new ArrayList<BankAccount>();

// 2)
// the reverse side: a BankAccount declares it's owner

@ManyToOne (cascade={CascadeType.PERSIST, CascadeType.MERGE })
   // changed to List/Bag on the Contact side
   @JoinColumn(name = "CONTACT_ID", nullable=false, updatable=false, insertable=false) // see Hibernate book, p. 294!!
   @org.hibernate.annotations.ForeignKey(name="FK_CONTACT_BANKACCNT_ID")
private Contact contact;


Note, I do not think that it makes any differences if I used a List here instead of a Set; in other relations I'm working with Sets.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 12:35 pm 
Newbie

Joined: Tue May 27, 2008 3:46 pm
Posts: 5
I will give it a try, and let you know if it works. Thank you very much for the help!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 3:25 pm 
Newbie

Joined: Tue May 27, 2008 3:46 pm
Posts: 5
The problem got solved.

I find this post to be very useful:
http://forum.hibernate.org/viewtopic.php?t=937513

Especially the last entry.

I did change from 'Set' to 'List', and also using merge() in my DAO object.

I, however, removed "updatable=false, insertable=false" in my child object.

These apparently made the difference.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 2:11 pm 
Newbie

Joined: Wed Aug 13, 2008 6:09 am
Posts: 4
Hello!

In my application i'm facing the same issue. Like you i'm using flex, and when i'm only working on the server side my tests run well.

The reason why this doesn't work properly is i think because the proxy (persistentSet) set by hibernate to replace the List is lost after a server/client
exchange, and thus hibernate is not able to see if some delete occured. But as the (still present) items in the list have Ids, they can be updated.

My problem when using merge() instead of update is that every item in my set are deleted and then re-inserted. And if the method seem to delete the elements i have deleted is only because they're not present in the set i give to hibernate when calling the update methode

Does merge() do the same thing in your application? I find this a little bit ugly since i save a graph. So a child could have a lots of children. To delete and re-insert every objects on each update is inconceivable.

As the parent should still be in the session, isn't it possible to use it to compare if an element have been removed ?

I'm quite new with those thing and i might be wrong about session and transaction stuffs when speaking about those.

thx


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2008 5:11 am 
Newbie

Joined: Wed Aug 13, 2008 6:09 am
Posts: 4
an update from the version 3.1.3 to 3.2.5 solved the problem of the deletion of all items in the association


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