Entity Lifecycle, Persistence Context, Session vs EntityManager
Learn Entity Lifecycle, Persistence Context, Session vs EntityManager (Phase 2 · Hibernate Core Concepts (Essential), Hibernate) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
Entity states • Transient (new) : created with new , no id (or not associated), not in the database, unknown to Hibernate. • Persistent (managed) : associated with an open persistence context (Session/EntityManager). Hibernate tracks it: changes are detected ( dirty checking ) and written at flush/commit. Returned by find() , queries, and after persist() . • Detached : has a database identity but its persistence context was closed/cleared; changes are not tracked. Reattach with merge() . • Removed : scheduled for deletion at flush ( remove() ).