-->
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: Removals from collection are not persisted
PostPosted: Wed Aug 30, 2006 1:03 pm 
Newbie

Joined: Thu Jun 01, 2006 11:31 am
Posts: 9
I am having a problem removing elements from a collection and having the updated collection persisted correctly when using a OneToMany mapping.

Here's the mapping as specified in the 'parent' class:

Code:
Class ParentImpl {

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="relatedMember", targetEntity=MemberImpl.class)     
public Collection<Member> getMembers() { return members;}

}

What I'm finding is that when I remove Member instances from the collection held ParentImpl, when I persist ParentImpl, the references removed from that collection are not removed from the MemberImpl table. When I reload the ParentImpl call members I removed from the collection, are repopulate from the MemberImpl table.

I understand that, since I'm not removing the parent instance, then it might follow that the cascade doesn't have to propagate the removal of the related member instances however, if I UPDATE the collection with more members, and persist ParentImpl, then a cascade does occur, as the new members appear in the MemberImpl.

So, it appears that for create/update operations, cascading works perfectly, however for delete operations ,it only appears to propagate if the parent instance is deleted.
This strikes me as wrong - how can I set up EJB3 to allow me to remove entries from the collection, and have those entries removed from the related table without removing the parent instance.

As a comparison, I tried the same operation using the Hibernate Core implementation using the following mapping:

Code:
   <set name="members" cascade="all-delete-orphan" inverse="true" lazy="true">
       <key column="memberID"/>
       <one-to-many class="MemberImpl"/>
   </set>


This behaves correctly, when I removed members from the collection, these removals were correctly unpersisted in the 'member' table.

So, my question then, is how do I configure EJB3 EntityManager annotations to succesfully propagate removals from a collection, when the parent instance holding the collection is not being deleted.

This is especially frustrating when the Hibernate core implementation appears to support this perfectly.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 2:57 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
@org.hibernate.annotations.CascadeType(DELETE_ORPHAN)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 3:18 pm 
Newbie

Joined: Thu Jun 01, 2006 11:31 am
Posts: 9
Hello Emmanuel,

DELETE_ORPHAN?

How would I use this with my EJB annotation, when I tried to do cascade=CascadeType.DELETE_ORPHAN, I get an error.

Forgive me if I'm doing something wrong here - I've very new to EJB3 persistence - can you advise me how I'd use this cascade type?

-Thom


Top
 Profile  
 
 Post subject: Re: Removals from collection are not persisted
PostPosted: Wed Aug 30, 2006 3:44 pm 
Newbie

Joined: Wed Aug 16, 2006 7:30 pm
Posts: 7
instead of

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="relatedMember", targetEntity=MemberImpl.class)
public Collection<Member> getMembers() { return members;}

}


Try

@OneToMany(fetch=FetchType.LAZY, mappedBy="relatedMember", targetEntity=MemberImpl.class) @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
public Collection<Member> getMembers() { return members;}

}


Ethan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 4:27 pm 
Newbie

Joined: Thu Jun 01, 2006 11:31 am
Posts: 9
wow.

Thanks, clearly I have much to learn!

Though it strikes me that the EJB cascade defaults need some work for, without the Hibernate extensions, I;d be out of luck!

Thanks again

-Thom


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 12:15 pm 
Newbie

Joined: Thu Jun 01, 2006 11:31 am
Posts: 9
After mulling this fix - it occurs to me that there is a short-coming with the default implementation of cascade deletes with the EJB 3 EntityManager implementation.

The above scenario demonstrates that there is a gap in what the EJB3's implementation of cascading delete operations can cover.

Do you think it is valid to raise this as a shortcoming with EJB3 on the relevent sun forum? (b.t.w. - if anyone knows the appropriate forum to raise this issue, please do tell!)

-Thom


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 3:33 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
ejb3-feedback@sun.com

_________________
Emmanuel


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.