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.