Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Cascade.remove performance suggestions
icon5.gif  Cascade.remove performance suggestions [message #1107540] Thu, 12 September 2013 11:10 Go to next message
Klaus G ønbæk is currently offline Klaus G ønbæk
Messages: 3
Registered: January 2010
Junior Member
When you have a OneToMany relation, you can use Join fetch and batch fetch to optimize the loading of the child collection. However when you delete the parent object, and Cascade.REMOVE causes the children to be removed, each child is removed in its own query
DELETE FROM CHILD WHERE ID = ? 

Why is cascading delete not implemented as a single
DELETE FROM CHILD WHERE PARENT_ID = ? 

I can easily write a custom delete query, but that means that other developers need to be aware that entityManager.remove() should never be used on objects of type Parent. Furthermore, if the Parent is a child in another OneToMany I can't use remove() on the Parent's parent...( and I have 8 levels of OneToMany)
The current behavior is probably needed to keep the UnitOfWork in a consistent state, but it would be nice to be able to configure you own removal query, regardless if the entity was removed as part of cascading or a direct call to remove.
In my case @CascadeOnDelete wont work since the same JPA classes have work on 7 different databases and not all of them supports this feature.

Does anyone have experience in this area?

[Updated on: Thu, 12 September 2013 11:11]

Report message to a moderator

Re: Cascade.remove performance suggestions [message #1110924 is a reply to message #1107540] Tue, 17 September 2013 10:11 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

EclipseLink does support deleting a private owned OneToMany in a single query, but this depends on several things.

- you must set the relationship as @PrivateOwned or orphan-removal=true
- you need to use the @DeleteAll annotation on the relationship, or configure the mapping in a customizer
- the child can't use optimistic locking (because lock must be checked), or multiple tables, inheritance or have its own private owned relationships (because they also need to be removed)
- also note that an removal events may not be fired if you delete all of the children

Otherwise, you could use a delete JPQL query to delete the children or tree.


James : Wiki : Book : Blog : Twitter
Previous Topic:NullPointerException from moxy due to corrupted CycleDetectionStack
Next Topic:Bidirectional one-to-one relations with dynamic entities
Goto Forum:
  


Current Time: Fri Aug 01 22:36:38 EDT 2014

Powered by FUDForum. Page generated in 0.01589 seconds