The @Entity annotation tells Hibernate that a Java class is a persistent entity: instances of the class represent rows in a database table, and Hibernate loads, tracks and saves them for you. Without @Entity Hibernate ignores the class.
@Entity comes from the Jakarta Persistence (JPA) specification, not from Hibernate itself, so the same annotation works with any JPA provider. Hibernate 6 and 7 use the jakarta.persistence package. Older Hibernate 5 and Java EE applications use javax.persistence, which is the only difference in the code.
Marking a class with @Entity has four effects:
Session or EntityManager, which gives you persist, find, merge and remove, plus automatic dirty checking.select u from User u.The entity name defaults to the unqualified class name. You can change it with @Entity(name = "AppUser"), which only affects query strings, not the table name.
A working entity needs @Entity, an identifier marked with @Id, and a no-argument constructor that is at least protected.
Hibernate maps this class to a table named User (or user, depending on the naming strategy and database), with columns id, username and passwordHash. Spring Boot applies a snake-case naming strategy by default, so you get user and password_hash.
Store the password as a hash, never as plain text. The original example of a password field is only acceptable as a mapping illustration.
By default the table name comes from the entity class. Use @Table to set a different name, a schema or constraints. This is also the usual fix when the class name is a reserved word in your database, such as User in PostgreSQL.
@Entity says the class is persistent. @Table and @Column only adjust how it is mapped.
The JPA specification sets these requirements:
| Requirement | Reason |
|---|---|
Annotated with @Entity (or declared in orm.xml) | Marks the class as persistent |
Has an @Id or @EmbeddedId attribute | Primary key for the row |
| Top-level class, not an interface or enum | Only classes can be entities |
Not final, with no final persistent fields | Hibernate creates proxies and subclasses for lazy loading |
| Has a public or protected no-argument constructor | The provider instantiates entities by reflection |
Records cannot be entities because they are final and have no setters. Use a record for a projection or DTO, and keep the entity as a regular class.
Once a class is an entity, you can link it to other entities with @OneToOne, @OneToMany, @ManyToOne and @ManyToMany. These annotations belong on the fields of the entity and each target type must itself be an @Entity.
The Order entity is mapped to a table called orders because ORDER is a reserved SQL word. Once instances are loaded through a Session they become managed, and Hibernate writes pending changes to the database when the transaction flushes.
@Entity from the wrong package. Spring Boot 3 and Hibernate 6 or later need jakarta.persistence.Entity; a javax.persistence.Entity import is silently ignored by the new stack and you get an "Unknown entity" error.AnnotationException: No identifier specified for entity.final or using a record. Lazy proxies cannot be created.@EntityScan.org.hibernate.annotations.Entity. It was removed long ago; use the JPA annotation.It is a JPA annotation defined in jakarta.persistence. Hibernate is one implementation of the specification, so the same class works with EclipseLink or OpenJPA.
No. Without @Table the table name is derived from the entity name. Add it when the name differs, when you need a schema, or when the default name is a reserved word.
No. Every entity needs a primary key, either a single @Id, a composite @IdClass, or an @EmbeddedId.
An entity has its own identity and table. An @Embeddable type has no identity and its fields are stored as columns in the owning entity's table.
@Entity turns a plain Java class into a persistent type that Hibernate maps to a table and manages through a persistence context. Pair it with @Id, keep a no-argument constructor, avoid final classes, and use the jakarta.persistence package on current Hibernate and Spring Boot versions. Use @Table, @Column and relationship annotations to refine the mapping.