The @Entity annotation marks a Java class as a JPA entity: a class whose instances the persistence provider (Hibernate, EclipseLink) can map to rows of a database table, load, track and save. Without @Entity, the provider ignores the class, and EntityManager operations on it fail with an IllegalArgumentException stating that the type is not a known entity.
Since Jakarta EE 9 (Jakarta Persistence 3.0) the annotation lives in jakarta.persistence. Older code and tutorials use javax.persistence, which is the same API under its former package name.
@Entity is a marker annotation with one optional attribute, name. It tells the provider three things:
select e from Employee e.An entity needs an identifier, declared with @Id (or @EmbeddedId / @IdClass). A class with @Entity but no identifier fails at startup with a mapping error.
The Employee class now maps to a table, its fields map to columns, and id is the primary key. The provider generates the SQL for inserts, updates, deletes and selects.
The Jakarta Persistence specification sets requirements that the provider checks or relies on:
| Requirement | Why it matters |
|---|---|
| A public or protected no-argument constructor | The provider creates instances reflectively when loading rows. |
| A top-level or static nested class, not an interface or enum | Only classes can be entities. |
The class is not final, and persistent methods and fields are not final | Hibernate generates proxies and bytecode-enhanced subclasses for lazy loading. |
| A persistent identifier | The provider needs it to find, cache and update the row. |
| Java records cannot be entities | They are final and immutable; records can be used as embeddables in newer provider versions. |
If the no-argument constructor is missing, the failure is reported when the entity is instantiated or at startup, depending on the provider. Declaring the constructor protected keeps application code from using an invalid, half-initialised object.
By default the table name is the entity name, which defaults to the unqualified class name. Hibernate with Spring Boot applies a naming strategy that turns EmployeeProfile into employee_profile. Override the defaults explicitly when the schema already exists.
Here @Entity(name = "Staff") changes the name used in JPQL (select s from Staff s), while @Table(name = ...) changes the database table. These are two separate things and are often confused. Fields marked @Transient are skipped.
An entity becomes useful when you hand it to an EntityManager. Instances move through four lifecycle states: new (transient), managed, detached and removed.
persist makes the new object managed, so the insert runs at flush or commit. find returns a managed instance or null. While an entity is managed, changing a field is enough for the provider to issue an UPDATE at commit (dirty checking).
Both sides of an association must be entities. Relationship annotations such as @OneToMany, @ManyToOne, @OneToOne and @ManyToMany describe how entities relate through foreign keys or join tables.
In Employee you would add @ManyToOne(fetch = FetchType.LAZY) private Department department;. The mappedBy attribute marks Employee.department as the owning side that holds the foreign key. Use lazy fetching on associations by default to avoid loading large object graphs.
javax.persistence imports on a Jakarta EE 9+ or Spring Boot 3+ project; the annotation is then not recognised. Use jakarta.persistence.final, or making Kotlin classes final without the kotlin-jpa and allopen compiler plugins.equals and hashCode over all fields; use the identifier or a business key instead.@EntityScan.Yes. A class must be an entity, or an embeddable or mapped superclass used by an entity, to be handled by the persistence provider. An undeclared class passed to persist causes an IllegalArgumentException.
Yes. An abstract class can be an entity, and it takes part in inheritance mappings and polymorphic queries. It cannot be instantiated directly, only through its concrete subclasses.
@Entity declares that a class is persistent and gives it an entity name for JPQL. @Table only sets the database table, schema or constraints the entity maps to.
Not for JPA itself. With field access (the annotation is on fields) the provider reads and writes fields directly. Accessors are a design choice, and setters for the identifier are often omitted.
@Entity turns a plain Java class into a persistent type that a JPA provider maps to a table, tracks inside a persistence context and queries through JPQL. Give the class an @Id, a non-private no-argument constructor and non-final members, use jakarta.persistence imports on current stacks, and keep @Entity(name) and @Table(name) separate in your mind.