In Hibernate, the @Entity annotation marks a class as a persistent entity, so Hibernate maps it to a table and tracks its state in a Session. It is the standard jakarta.persistence.Entity annotation; Hibernate does not use a separate one of its own in current versions.
What Hibernate adds on top of the annotation is entity lifecycle management: an instance moves between transient, managed, detached and removed states, and changes to managed instances are written to the database automatically when the transaction commits.
Use jakarta.persistence.Entity with Hibernate 6 and 7, and javax.persistence.Entity with Hibernate 5. Hibernate used to ship its own org.hibernate.annotations.Entity, which was deprecated long ago and should not be used; its useful options are now separate annotations such as @DynamicUpdate, @Immutable and @Cache.
The class must have an identifier, a non-private no-argument constructor, and must not be final.
Hibernate only maps classes it knows about. In a plain Hibernate application you register the entity class when you build the SessionFactory:
With Spring Boot, or with JPA bootstrapping and a persistence.xml, scanning finds @Entity classes automatically. The H2 driver must be on the classpath for this example.
An instance is always in one of four states:
| State | Meaning | How you get there |
|---|---|---|
| Transient | New object, unknown to Hibernate | new Book(...) |
| Managed (persistent) | Attached to an open Session | persist, find, get, query results |
| Detached | Was managed, its Session closed | Closing the session, detach |
| Removed | Scheduled for deletion | remove |
Hibernate keeps a snapshot of each managed entity. At flush time it compares the entity with the snapshot (dirty checking) and issues an update only when something changed:
For a detached instance, use merge to copy its state onto a managed one.
@DynamicUpdate generates update statements with only the changed columns, which helps on wide tables.@Immutable marks an entity as read-only, so Hibernate skips dirty checking and rejects updates.@Cache enables the second-level cache for an entity when a cache provider is configured.@NaturalId declares a business key you can load by efficiently.These are in org.hibernate.annotations and work only with Hibernate, so using them ties your mapping to it.
equals and hashCode carefully. Using the generated id alone breaks while the id is null; a natural id or a stable surrogate is safer.FetchType.LAZY for associations, and fetch what you need with a query or an entity graph.toString implementations that touch lazy collections, because they trigger queries or a LazyInitializationException.For annotation-based mapping, yes. The alternative is the older XML mapping, which is rarely used now.
An entity has its own identity and table. An embeddable has no identity and is stored in the columns of the owning entity's table.
The entity is probably detached or you changed it outside a transaction. Modify it while it is managed and commit the transaction, or call merge.
No. Dirty checking writes the change on flush, which happens at commit by default.
In Hibernate, @Entity is the standard Jakarta Persistence annotation that makes a class a mapped, managed type. Register it with the SessionFactory or let Spring Boot scan for it, follow the identifier and constructor rules, and rely on the persistence context and dirty checking to write changes. Add Hibernate-specific annotations only when you need them.