JPA annotations tell a persistence provider such as Hibernate how plain Java classes map to relational tables, columns, keys and relationships. They replace the XML mapping files of early JPA, keep the mapping next to the code it describes, and let the provider generate SQL, manage entity state and build the schema without boilerplate.
Since Jakarta Persistence 3.0 (the version used by Spring Boot 3 and later, and Hibernate 6), the annotations live in the jakarta.persistence package, not javax.persistence. Older tutorials, including many examples online, still show the javax imports, which no longer compile against current versions.
Annotations are metadata. The provider reads them at startup, builds a model of your entities and uses it to create SQL for inserts, updates, deletes and queries. They fall into a few groups:
| Group | Common annotations |
|---|---|
| Entity and table mapping | @Entity, @Table, @Id, @GeneratedValue, @Column, @Enumerated, @Embedded, @Embeddable |
| Relationships | @OneToOne, @OneToMany, @ManyToOne, @ManyToMany, @JoinColumn, @JoinTable |
| Inheritance | @Inheritance, @MappedSuperclass, @DiscriminatorColumn |
| Queries | @NamedQuery, @NamedNativeQuery |
| Concurrency and lifecycle | @Version, @PrePersist, @PostLoad, @Transient |
A minimal entity needs @Entity and an @Id. Everything else has sensible defaults, and you override only what differs from the schema.
The entity needs a no-argument constructor that is at least protected, and the class must not be final. @GeneratedValue with IDENTITY relies on an auto-increment column, while SEQUENCE is usually the better choice on PostgreSQL and Oracle because it allows batching inserts.
@Column(nullable = false) and unique = true add constraints to the generated schema and are used by the provider for validation hints, but they are not a default-value mechanism. To set a database default, use columnDefinition or, better, a schema migration tool such as Flyway or Liquibase.
Relationships are mapped with annotations on fields that refer to other entities. The side that holds the foreign key is the owning side.
mappedBy = "user" says the Order.user field owns the relationship, so the foreign key is the user_id column on orders. @ManyToOne is eager by default, which often causes unnecessary joins, so setting FetchType.LAZY is a common best practice. @OneToMany is lazy by default. Fetch types are the standard JPA mechanism; Hibernate's own @Fetch annotation is a provider-specific extra, not part of JPA.
Keep both sides of a bidirectional relationship in sync with helper methods such as addOrder(Order), because the provider only looks at the owning side when writing foreign keys.
Bean Validation annotations such as @NotNull, @Size and @Email come from jakarta.validation.constraints, a separate specification. JPA providers integrate with it and run validation before persisting, but these annotations do not replace column constraints.
Use both layers: validation gives clear error messages early, while column constraints protect the data when other applications write to the same database. Hibernate can also derive some DDL constraints from validation annotations when it generates a schema.
@NamedQuery declares a JPQL query on the entity and lets the provider validate it at startup:
@Version adds optimistic locking: the provider checks and increments a version column on each update and throws an OptimisticLockException if another transaction changed the row. Lifecycle callbacks such as @PrePersist and @PreUpdate run code at defined points, which is handy for audit timestamps.
In Spring Data JPA you often skip named queries and declare methods such as Optional<User> findByEmail(String email) on a repository interface instead.
@Table on every entity?No. Without it the table name defaults to the entity name, subject to the naming strategy. Use @Table when the table name differs, or to add a schema or unique constraints.
@Id and @GeneratedValue?@Id marks the primary key field. @GeneratedValue tells the provider to generate its value, using identity, sequence or another strategy.
@Transient?It excludes a field from persistence, for example a value calculated at runtime. Fields marked with the transient keyword are also ignored.
Placement of @Id decides the access type: on a field means field access, on a getter means property access. Field access is the common choice and avoids side effects from getter logic.
JPA annotations map classes to tables and fields to columns, define relationships and inheritance, and configure queries, versioning and lifecycle hooks. Use jakarta.persistence imports on current stacks, prefer lazy fetching for @ManyToOne, combine validation annotations with real column constraints, and manage the schema with migrations in production.