The @Table annotation in JPA tells the persistence provider which database table an entity maps to. Without it, the entity is mapped to a table named after the entity class (subject to your naming strategy). With it, you can set the table name, schema, catalog, unique constraints and indexes explicitly.
It is part of the Jakarta Persistence API (jakarta.persistence.Table, formerly javax.persistence.Table before Jakarta EE 9). It is used together with @Entity, and it only changes the mapping of the primary table of the entity.
Here Customer is stored in the table customers. Without @Table, Hibernate with Spring Boot's default naming strategy would use customer. Use the annotation when the table already exists under a different name, when your organisation uses plural or prefixed table names, or when you want the mapping to be explicit.
| Attribute | Purpose | Default |
|---|---|---|
name | Name of the table | The entity name |
catalog | Database catalog the table belongs to | Provider or connection default |
schema | Database schema the table belongs to | Provider or connection default |
uniqueConstraints | Table-level unique constraints, including multi-column ones | None |
indexes | Indexes created with the table | None |
check | Table-level check constraints (added in Jakarta Persistence 3.2) | None |
comment | Table comment (added in Jakarta Persistence 3.2) | None |
options | Extra SQL appended to the table definition (added in 3.2) | None |
The last three attributes require a provider that implements Jakarta Persistence 3.2; check your Hibernate version before relying on them.
The generated SQL then refers to sales.orders. Setting the schema per entity is handy for multiple schemas in a single database. If every entity lives in one schema, a global setting such as Hibernate's hibernate.default_schema is simpler than repeating it.
columnNames and columnList use database column names, not Java field names. These definitions are used only when the provider generates the schema, for example with jakarta.persistence.schema-generation.database.action or Hibernate's hbm2ddl setting. They do not change an existing database. In production, manage the schema with a migration tool such as Flyway or Liquibase and keep the annotations in sync as documentation and for tests.
For a single-column constraint, @Column(unique = true) is shorter. Use uniqueConstraints for composite keys.
Spring Boot configures CamelCaseToUnderscoresNamingStrategy for Hibernate, so a field createdAt maps to created_at, and an entity OrderItem maps to order_item when @Table is absent. A name you give in @Table(name = ...) is passed through the physical naming strategy, which can still change case or add underscores.
Reserved words are a common source of errors. A table called order or user fails on many databases. Rename the table (orders, users) or quote it with backticks inside the name, which Hibernate converts to the database's quoting style:
Renaming is the more portable choice.
@Table configures only the primary table. Extra tables for one entity are declared with @SecondaryTable, and element collections or join tables use @CollectionTable and @JoinTable. With InheritanceType.JOINED, each subclass can have its own @Table; with SINGLE_TABLE, only the root entity's @Table applies.
No. If you omit it, the provider maps the entity to a table named after the entity class, adjusted by the naming strategy. Add it when the names differ or you need constraints and indexes.
Only if schema generation is enabled. The annotation describes the mapping; the DDL is executed when the provider's schema generation runs.
@Entity(name = ...) sets the entity name used in JPQL queries. @Table(name = ...) sets the database table name.
Use jakarta.persistence. It applies to Spring Boot 3 and later, Hibernate 6 and later, and Jakarta EE 9 and later. javax.persistence belongs to older stacks.
@Table maps an entity to a specific table and lets you set its name, schema, catalog, unique constraints and indexes. Use it whenever the default naming does not match your database, prefer a migration tool for real schema changes, avoid reserved words in table names, and import it from jakarta.persistence on any current stack.