Cascade.remove performance suggestions [message #1107540] |
Thu, 12 September 2013 15:10 |
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 15:11] Report message to a moderator
|
|
|
Re: Cascade.remove performance suggestions [message #1110924 is a reply to message #1107540] |
Tue, 17 September 2013 14:11 |
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.02959 seconds