@Table Annotation in JPA
Fast recall
Quick revision
Core answer
Whether you know when @Table is required and that composite uniqueness needs uniqueConstraints, not @Column
Skipping @Table when the table is plural/snake_case or in a non-default schema, or using @Column(unique) for multi-column uniqueness
Mention uniqueConstraints for multi-column uniqueness (e.g. email + tenantId) and schema use in multi-tenant apps
Interview-ready guide
Interview answer
For example, @Table(name="customer_account", uniqueConstraints=@UniqueConstraint(columnNames={"tenant_id","email"})) maps CustomerAccount and expresses email uniqueness within a tenant. @Column(unique=true) on email alone would describe global email uniqueness instead. A migration should still create and name the actual production constraint.
@Table Annotation in JPA
Direct Answer
@Table customizes the primary table used by an entity, including its name, schema, catalog, unique constraints, and indexes in schema-generation metadata. Use it when defaults do not match the database. The default schema is whatever the connection/provider/database resolves; it is not universally a schema named public.
Code Example
import jakarta.persistence.*;
@Entity
@Table(
name = "users", // class User → table users
schema = "sales", // lives in the sales schema
uniqueConstraints = @UniqueConstraint(
name = "uq_email_per_tenant",
columnNames = {"email", "tenant_id"} // unique TOGETHER
)
)
public class User {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String email;
@Column(name = "tenant_id")
private Long tenantId;
}
Common Mistakes & Pitfalls
- Skipping
@Tablewith plural names — classUserwon't find tableusers; setname. - Forgetting
schema— tables insales.ordersaren't auto-discovered; specify the schema. @Column(unique=true)for composite uniqueness — makes each column unique individually; use@Table(uniqueConstraints=...).- Relying on naming strategy blindly — Spring's implicit naming may or may not match your existing schema.
Check your understanding
Answer these without looking back at the article.
- What is the default table name for a @Entity class in Spring Boot?
- How does Spring Boot's naming strategy affect entity-to-table mapping?
- How do you create a multi-column unique constraint in JPA?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.