Note that there are some explanatory texts on larger screens.

plurals
  1. POBest practice when removing entity regarding mappedBy collections?
    text
    copied!<p>I'm still kind of undecided which is the best practice to handle <code>em.remove(entity)</code> with this entity being in several collections mapped using <code>mappedBy</code> in JPA.</p> <p>Consider an entity like a <code>Property</code> that references three other entities: a <code>Descriptor</code>, a <code>BusinessObject</code> and a <code>Level</code> entity. The mapping is defined using <code>@ManyToOne</code> in the Property entity and using <code>@OneToMany(mappedBy...)</code> in the other three objects. That inverse mapping is defined because there are some situations where I need to access those collections.</p> <p>Whenever I remove a Property using <code>em.remove(prop)</code> 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.</p> <p>The inverse mappings may become quite large and though I don't want to use something like <code>descriptor.getProperties().remove(prop)</code> because it will load all those properties that might have been lazy loaded until then.</p> <p>So my currently preferred way is to refresh the entity if it is managed: <code>if (em.contains(descriptor)) em.refresh(descriptor)</code> - which unloads a possibly loaded collection and triggers a reload upon the next access.</p> <p>Is there another feasible way to handle all those mappedBy collections of already loaded entites?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload