Hibernate / EclipseLink Interview questions
Explain the lifecycle of a JPA entity managed by EclipseLink?
Every entity instance passes through one of four states relative to a given persistence context, and understanding the transitions between them explains a lot of otherwise-confusing EclipseLink behavior.
New (transient) instances exist only in memory, created with plain new, and EclipseLink knows nothing about them. Calling em.persist() transitions an instance to managed, at which point EclipseLink tracks it, and any field changes made while managed are automatically detected and scheduled for a database update at commit time, without an explicit save call being necessary for updates.
Detached entities were once managed but no longer have an active persistence context tracking them, typically because the EntityManager closed or detach() was called explicitly; changes made to a detached entity aren't tracked and won't be persisted unless the entity is reattached via em.merge(), which copies its state onto a (possibly newly loaded) managed instance. Calling em.remove() marks a managed entity as removed, scheduling a delete at commit time, though the object itself still exists in memory until then.
More Related questions...