The @Table annotation tells JPA which database table an entity maps to. With no arguments, the table name is derived from the entity name. With @Table you set the table name, the schema and catalog it lives in, and optional uniqueConstraints and indexes that schema generation creates.
It lives in jakarta.persistence for JPA 3.0 and later (Spring Boot 3 and Hibernate 6) and in javax.persistence for older versions. Only the package name differs; the attributes are the same.
@Table is placed on a class that is already marked @Entity. It is optional. Without it, the persistence provider maps the entity to a table named after the entity, which is the unqualified class name unless you change it with @Entity(name = "...").
In Spring Boot with Hibernate, the physical name then passes through a naming strategy. The default CamelCaseToUnderscoresNamingStrategy turns OrderItem into order_item. An explicit @Table(name = "OrderItem") is processed by the same strategy, so it also ends up as order_item unless you quote the name or change the strategy. Keep this in mind when a table seems to have a different name than the one you wrote.
| Attribute | Purpose |
|---|---|
name | Table name. Defaults to the entity name |
schema | Database schema containing the table |
catalog | Database catalog containing the table |
uniqueConstraints | Array of @UniqueConstraint rules, used for schema generation |
indexes | Array of @Index definitions, used for schema generation |
JPA 3.2 adds a few more attributes, such as check constraints and comments, but support depends on your provider version, so check its documentation before using them.
This maps Product to the products table in the catalog schema, so SQL generated by Hibernate refers to catalog.products. Use schema for PostgreSQL, SQL Server and Oracle schemas. MySQL treats schema and database as the same thing, so use catalog or leave both out and set the database in the connection URL.
You can also set a default schema globally with spring.jpa.properties.hibernate.default_schema, which avoids repeating it on every entity.
Name your constraints and indexes explicitly. Otherwise the database generates names such as UK_6dotkott2kjsp8vw4d0m25fb7, which make error messages and migrations harder to read. Use the column names in the database, not the Java field names, in columnNames and columnList.
For a single column you can write @Column(unique = true), but @UniqueConstraint is the way to express a composite rule across several columns.
Unique constraints and indexes declared on @Table are only applied when the persistence provider generates the schema, for example with spring.jpa.hibernate.ddl-auto=create or update. They are not enforced by JPA at runtime. If the real database lacks the constraint, duplicates get in.
For production systems, manage the schema with a migration tool such as Flyway or Liquibase, and use ddl-auto=validate or none. Treat the annotations as documentation of what the migration should create, and make sure the two agree.
Other points:
Order maps to the SQL keyword order. Use @Table(name = "orders") to avoid the problem, or quote it with @Table(name = "\"order\"").@Table on the root entity affects all subclasses in the SINGLE_TABLE strategy, and each entity gets its own table with JOINED.@SecondaryTable. For join tables of many-to-many relationships, use @JoinTable, not @Table.No. Without it, the entity maps to a table with the default name. Add it when you need a different name, a schema, or constraints and indexes.
Use jakarta.persistence with Spring Boot 3 and later. The javax.persistence package belongs to older Spring Boot 2 and JPA 2.x applications.
Spring Boot sets a physical naming strategy that converts camel case to underscores. Set spring.jpa.hibernate.naming.physical-strategy to org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl to keep names exactly as written.
Only if the schema is created from them. Always make sure the actual database has the matching constraint, typically through a migration script.
@Table customises the table an entity maps to: its name, schema, catalog, unique constraints and indexes. It is optional, and its constraints only matter for schema generation. Use jakarta.persistence on current stacks, name your constraints, remember Spring Boot's naming strategy, and manage production schemas with migrations so the database enforces the rules.