Best practice when removing entity regarding mappedBy collections?
Posted
by Daniel Bleisteiner
on Stack Overflow
See other posts from Stack Overflow
or by Daniel Bleisteiner
Published on 2010-04-08T10:34:36Z
Indexed on
2010/04/08
14:13 UTC
Read the original article
Hit count: 171
I'm still kind of undecided which is the best practice to handle em.remove(entity)
with this entity being in several collections mapped using mappedBy
in JPA.
Consider an entity like a Property
that references three other entities: a Descriptor
, a BusinessObject
and a Level
entity. The mapping is defined using @ManyToOne
in the Property entity and using @OneToMany(mappedBy...)
in the other three objects. That inverse mapping is defined because there are some situations where I need to access those collections.
Whenever I remove a Property using em.remove(prop)
this element is not automatically removed from managed entities of the other three types. If I don't care about that and the following page load (webapp) doesn't reload those entities the Property is still found and some decisions might be taken that are no longer true.
The inverse mappings may become quite large and though I don't want to use something like descriptor.getProperties().remove(prop)
because it will load all those properties that might have been lazy loaded until then.
So my currently preferred way is to refresh the entity if it is managed: if (em.contains(descriptor)) em.refresh(descriptor)
- which unloads a possibly loaded collection and triggers a reload upon the next access.
Is there another feasible way to handle all those mappedBy collections of already loaded entites?
© Stack Overflow or respective owner