In JPA, the @Entity annotation declares that a class is a persistent entity: the JPA provider (Hibernate, EclipseLink or another) maps it to a database table and manages its instances through an EntityManager. Each instance corresponds to one row, and each persistent field corresponds to a column.
The annotation lives in the jakarta.persistence package since Jakarta Persistence 3.0. Older code that targets Java EE or Hibernate 5 uses javax.persistence. The behaviour is the same; only the package name changed.
@Entity is a marker with a single optional element, name. When a provider starts, it builds its metamodel from every class that is annotated with @Entity (or declared as an entity in orm.xml) and that belongs to the persistence unit. Only those classes can be:
EntityManager.persist,find or queried with JPQL and the Criteria API,A class without @Entity is just a Java class as far as JPA is concerned.
The provider maps Employee to a table whose name comes from the entity name, with columns id, name and department. The exact table name depends on the naming strategy of your provider or framework.
The entity name is used in JPQL. The table name is used in SQL. They are separate settings.
If you omit both, the entity name is the unqualified class name and the table name is derived from it. Changing @Entity(name = ...) does not rename the table.
| Rule | Detail |
|---|---|
| Annotation | @Entity on the class, or an entry in orm.xml |
| Identifier | At least one @Id, or an @EmbeddedId or @IdClass |
| Constructor | A public or protected no-argument constructor |
| Class modifiers | Top-level, not final; no final persistent fields or methods |
| Type | A class, not an interface, enum or record |
| Serialization | Implement Serializable only if you pass detached entities across remote boundaries |
The no-argument constructor and the non-final rule exist because providers instantiate entities by reflection and generate subclasses as proxies for lazy loading.
Marking a class with @Entity makes its instances take part in a lifecycle with four states:
new, not yet known to the provider.persist, find or a query. Changes are detected automatically and written at flush or commit.merge.The setDepartment change is saved because the entity is managed: this is dirty checking. In a Spring application you normally let the @Transactional boundary and a Spring Data repository handle the EntityManager for you.
The placement of @Id decides the access type. Annotating a field means the provider reads and writes fields directly; annotating a getter means property access. Keep one style per class hierarchy.
Entities can inherit from other entities or from a @MappedSuperclass that carries shared columns such as createdAt. Relationships between entities use @OneToOne, @OneToMany, @ManyToOne and @ManyToMany, and both sides must be entities. Prefer FetchType.LAZY on to-one associations, since the JPA default for them is eager.
With Spring Boot, entities in the application's base package are found automatically; use @EntityScan for other packages. In plain Java SE or Jakarta EE, list them in persistence.xml:
If an entity is missing from the unit, queries fail with an error such as "Employee is not mapped".
No. Entities must be non-final classes with a no-argument constructor and mutable state that the provider can manage. Use records for read-only projections returned from queries.
Not by itself. The table is created by schema generation (jakarta.persistence.schema-generation.database.action or Hibernate's hbm2ddl) or, better for production, by a migration tool such as Flyway or Liquibase.
@Entity makes the class persistent and sets the JPQL name. @Table only configures the physical table name, schema and constraints.
Usually the class is missing @Entity, is outside the scanned packages, or you imported the wrong package (javax versus jakarta). Check all three.
@Entity is the entry point of JPA mapping. It makes a class persistent, lets the provider manage its lifecycle, and allows it to be queried and related to other entities. Give each entity an identifier and a no-argument constructor, keep it non-final, use jakarta.persistence on current stacks, and manage the schema with migrations.