LearnThatStack Ace your next interview
Hibernate · question
Question 4 of 42

What are the different states of a Hibernate entity?

beginner
← All Hibernate questions
Re-explain
Visual ↓

An entity starts transient and becomes persistent when a session tracks it. Later it can become detached or removed.

  1. Transient. New, unknown to any session, and with no database row.
  2. Persistent. Also called managed. The session tracks it and saves its changes to its row.
  3. Detached. Its session closed, so changes are no longer tracked.
  4. Removed. Marked for deletion. Hibernate deletes the row at the next flush, when pending changes go to the database.
User user = new User(); // Transient state
session.persist(user);  // Persistent state
session.close();        // Detached state
user = newSession.merge(user); // Persistent again
newSession.remove(user);       // Removed state

The last two lines use a new session, because the first one is closed.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
See it - interactive diagram
The four states of a Hibernate entity
TransientNew object. No session knows it, and it has no row.
  • Persistent (managed)session.persist(user)
session.persist(user)
Persistent (managed)The session tracks it and saves its changes to its row.
  • Detachedsession.close()
  • RemovednewSession.remove(user)
session.close() → Detached
newSession.remove(user) → Removed
DetachedIts session closed, so its changes are no longer tracked.
  • Persistent (managed)newSession.merge(user) returns a copy
RemovedHibernate has marked it for deletion. The row stays until the next flush, when Hibernate sends pending changes to the database.
  • Row deletednext flush deletes the row
Removed next flush deletes the row
Row deletedNot a fifth state. The flush removed the row.
  • Detached Persistent (managed) (newSession.merge(user) returns a copy)

persist() makes a new object managed, close() detaches it, merge() returns a managed copy, and remove() deletes the row only at the next flush.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Hibernate cheatsheet.

← Back to all Hibernate questions
Pro · $10/mo

37 of 42 Hibernate answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime