@Entity is the Jakarta Persistence (JPA) annotation that declares a class as a persistent entity, meaning each instance maps to a row in a database table. A JPA provider such as Hibernate then manages its lifecycle: it inserts, loads, updates and deletes the object through an EntityManager.
An entity needs @Entity, an @Id field, and a non-private no-argument constructor. Everything else, such as the table name and column types, has sensible defaults you can override.
@Entity is in jakarta.persistence (Jakarta EE 9 and later, which includes Spring Boot 3 and newer). Older applications on Spring Boot 2 or Java EE use javax.persistence.Entity instead; the annotation is otherwise identical. The package rename is the most common reason an upgrade fails with "Not a managed type".
Marking a class with @Entity does three things:
persist, find or a query.In Spring Boot you then persist it through a Spring Data repository:
With Hibernate and the default naming strategy, the resulting SQL is similar to this:
The Jakarta Persistence specification requires that an entity:
@Entity (or declared in XML mapping),final, and has no final methods or persistent final fields,@Id or @EmbeddedId.Java records cannot be entities, because they are final and their fields are immutable. They can be used for projections, and as embeddable types where your provider supports it.
The name attribute changes the entity name used in JPQL, not the table:
The table comes from @Table. Without it, the provider uses the entity name and applies the configured naming strategy; Spring Boot's default converts camel case to snake case.
Spring Boot scans for entities in the package of your @SpringBootApplication class and below. If entities live elsewhere, add @EntityScan("com.example.domain") on a configuration class. In plain JPA, list them in persistence.xml or let the provider scan the persistence unit's root.
| Mistake | Symptom | Fix |
|---|---|---|
Missing @Id | AnnotationException about no identifier | Add @Id to a field or getter |
| Private no-arg constructor | Instantiation error when loading | Make it protected or public |
| Entity in an unscanned package | "Not a managed type" | Use @EntityScan |
javax import on Spring Boot 3 | Class is ignored | Switch to jakarta.persistence |
Using Lombok @Data | Broken equals, hashCode and toString on relations | Write them by hand, based on the id |
Mixing field and property access is also a trap: the placement of @Id decides whether the provider reads fields or getters.
No. @Entity declares the class as persistent; @Table only customises the table name, schema and constraints. @Table is optional.
Not in standard JPA. The provider creates instances reflectively and needs one that is protected or public.
No. Use a plain class, a record, or an interface projection with Spring Data. Marking a DTO as an entity maps it to a table.
Not within one persistence unit. Use @Entity(name = "...") to disambiguate classes with the same simple name from different packages.
@Entity turns a Java class into a managed persistent type that maps to a table. Add an @Id, keep a non-private no-argument constructor, avoid final, use the jakarta.persistence package on current stacks, and make sure the class is scanned. Everything beyond that, including table and column names, is optional customisation.