Enterprise Java Development@TOPIC@

Chapter 58. Relationship Capabilities

58.1. Fetching
58.1.1. fetch=LAZY
58.1.2. fetch=EAGER
58.2. Cascades
58.3. Orphan Removal
58.4. Summary


  • Same as fetch=LAZY case


fetch=EAGER does not always mean "done "efficiently"

fetch=EAGER is only a promise to fetch the related object before returning from the call. It does not always mean that a SQL JOIN was performed. There are times (e.g., fetch=EAGER on multiple collections within parent) when the provider will perform a SQL JOIN for some of the relationships and the functional equivalent of a fetch=LAZY for the remaining relationships.

Don't over user fetch=EAGER

Although useful at times, avoid haphazard use of fetch=EAGER when defining relationships and look to rely on custom queries to do this behavior instead. It is very easy to turn a fetch=LAZY into EAGER at query time. It is very difficult to do the opposite. Custom queries also allow us to incrementally build a complex parent object tree an optimized query at a time. fetch=EAGER will do the same, but may perform 10s, 100s, 1000s of more unoptimized queries of child rows we may never need.


PERSIST

Related entities persisted when this entity is passed to em.persist()

DETACH

Related entities detached from persistence unit when this entity passed to em.detach()

REMOVE

Related entities deleted from database when this entity passed to em.remove()

REFRESH

Related entities refreshed with state of database when this entity passed to em.refresh()

MERGE

Related entities update state of database when this entity is passed to em.merge()




  1. Attendee.residence_id set to null

  2. Orphaned residence deleted