The @Table annotation (jakarta.persistence.Table) tells JPA which database table an entity maps to. Without it, the provider uses the entity name as the table name, subject to the naming strategy. You add it when the table name, schema or catalog differs from the default, or when you want to declare unique constraints and indexes next to the entity.
It applies to the primary table of an entity. Since JPA 3.0 the package is jakarta.persistence; in Spring Boot 2 and older projects it is javax.persistence with the same attributes.
By default, an entity class OrderItem maps to a table derived from its name. Hibernate's Spring Boot naming strategy turns that into order_item, while plain JPA providers often use ORDERITEM. Relying on that is fragile, so most production code states the table explicitly.
@Table is placed on the entity class, next to @Entity:
This maps Customer to sales.customers. Everything you do not specify keeps its default.
| Attribute | Purpose | Default |
|---|---|---|
name | Table name | Entity name, after the naming strategy |
schema | Database schema that owns the table | Provider or connection default |
catalog | Database catalog | Provider or connection default |
uniqueConstraints | Array of @UniqueConstraint | None |
indexes | Array of @Index | None |
Some databases do not use schemas or catalogs the way others do. MySQL treats a schema and a catalog as the same thing as a database, so schema is usually left unset there and the database comes from the connection URL.
uniqueConstraints and indexes let the entity carry the DDL hints. They matter when the persistence provider generates the schema (for example in tests), and they document the intent even when migrations own the real schema.
Note that columnNames and columnList use database column names, not Java field names. Give constraints and indexes explicit names so they match your migration scripts and produce readable error messages.
With inheritance, @Table on the root class names the single table for SINGLE_TABLE. For JOINED and TABLE_PER_CLASS, each entity class can carry its own @Table. An entity that spans several tables uses @Table for the primary table and @SecondaryTable for the others.
Use @SecondaryTable sparingly. A one-to-one association is usually clearer.
An explicit @Table(name = "...") is still passed through the provider's physical naming strategy. In Spring Boot, CamelCaseToUnderscoresNamingStrategy lower-cases the name and converts camel case to underscores, so @Table(name = "orderItems") becomes order_items. If you need exact, case-sensitive or quoted names, configure the strategy rather than fighting it.
Avoid table names that are SQL reserved words, such as user, order or group. In PostgreSQL, user is reserved, which is a common reason for choosing app_user in @Table(name = "app_user"). Quoting is possible with hibernate.globally_quoted_identifiers=true, but it makes hand-written SQL harder.
No. Only @Entity and an identifier are required. Without @Table, the provider derives the table name from the entity name.
@Entity(name = "...") sets the entity name used in JPQL queries. @Table(name = "...") sets the physical table name in the database.
No. It only describes the mapping. Tables are created by schema generation (hibernate.hbm2ddl.auto or jakarta.persistence.schema-generation.database.action) or, better in production, by a migration tool such as Flyway or Liquibase.
Yes, but it is rarely a good idea. Prefer inheritance, @Embeddable types or projections to expose different views of one table.
Use @Table to name the table, schema and catalog explicitly, and to declare unique constraints and indexes that match your migrations. Remember the jakarta.persistence package on current Spring Boot and Hibernate versions, avoid reserved words as table names, and let a migration tool own the production schema.