October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

JPA Entity Lifecycle: Understand the Four States in Hibernate

JPA entity states describe an object’s relationship to a persistence context. Learn the four states, how entities transition, and why database changes may wait for flush or commit.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

JPA entities can be new, managed, detached, or removed. These states describe an object’s relationship to a persistence context—not simply whether a database row exists. In particular, an entity can be managed before its insert reaches the database, and a removed entity can remain in the context while its deletion is pending.

What the four entity states mean

Jakarta Persistence defines entity lifecycle operations relative to a persistence context. Hibernate uses closely related terms: it commonly calls a new entity transient and a managed entity persistent. The labels differ, but the central question is the same: is this Java object associated with the current persistence context?

As an Amazon Associate I earn from qualifying purchases.

State Identity and context What happens to changes
New (Hibernate: transient) No persistent identity and not associated with a persistence context. Changes are ordinary in-memory changes. Calling persist() makes the entity managed; an insert may wait until flush or commit.
Managed (Hibernate: persistent) Has persistent identity and is associated with the current persistence context. The context tracks persistent-state changes and synchronizes them when it flushes. An insert or update may still be pending.
Detached Has persistent identity but is no longer associated with the context. Changes are not automatically tracked by the context from which it became detached. merge() copies state to a managed instance and returns that instance.
Removed Has persistent identity, remains associated with the context, and is marked for deletion. Deletion is synchronized through flushing, typically at or before transaction commit.

The Jakarta Persistence 4.0 milestone 4 specification defines a managed entity as one with persistent identity that is currently associated with a persistence context. The milestone status matters: this is an M4 specification, not a claim about a final 4.0 release. See Jakarta Persistence 4.0 M4, section 3.6. For Hibernate’s terminology and context behavior, see the Hibernate ORM User Guide; it is a rolling guide, so consult the documentation for the Hibernate version used by your application when relying on implementation-specific behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How entities move between states

New to managed with persist()

A newly constructed object is new (transient in Hibernate terminology). Calling persist(entity) on it makes it managed by the applicable persistence context. That lifecycle change does not promise that an SQL INSERT runs at that exact call. The persistence context synchronizes its pending work when it flushes, which can occur before or during transaction completion depending on the circumstances.

Managed to detached

An entity becomes detached when it is no longer associated with the persistence context. Detachment can result from an explicit detach operation or from a context boundary such as clearing or closing the context. Once detached, edits to the Java object are not automatically tracked by that former context.

Detached to managed with merge()

merge(detachedEntity) copies the detached entity’s state into a managed instance and returns that instance. Do not assume the object passed to merge() itself becomes managed; use the returned value for subsequent work that needs the managed object. For detached input, Jakarta Persistence describes the returned instance as having the same persistent identity and state but a distinct Java object identity. See the specification’s merge semantics.

Managed to removed with remove()

Calling remove(entity) on a managed entity marks it for deletion. The object remains associated with the persistence context in the removed state until synchronization and lifecycle processing complete. The delete is not necessarily sent to the database at the moment remove() is called.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why flush and commit matter

A persistence context tracks changes in memory and synchronizes them with the database through a flush. Consequently, the four states are not a direct report of which SQL statements have already run:

  • A managed entity may be awaiting its initial insert.
  • Edits to a managed entity may be awaiting an update.
  • A removed entity may be awaiting a delete.

Flush is the synchronization point; transaction commit is a transaction boundary, not a guarantee that every lifecycle method executed SQL immediately. The precise timing can depend on the persistence provider, transaction setup, and flush behavior. Jakarta Persistence’s lifecycle and synchronization rules are set out in section 3.6 of the 4.0 M4 specification.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What to check when debugging an entity

  • New or transient: Was the object constructed but not persisted?
  • Managed or persistent: Is it associated with the current persistence context, so that context can track its changes?
  • Detached: Did it outlive, or get separated from, the context that managed it? If you merge it, are you using the returned object?
  • Removed: Was it marked for deletion while still managed, with database synchronization still pending?

Also check relationship mappings before expecting lifecycle operations to propagate to associated entities. Cascading occurs only where the mapping includes the relevant cascade type; it is not automatic for every association. See the Jakarta Persistence specification for the operation and cascade rules.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.