Spring Data JPA - @Table Annotation
Last Updated :
30 Oct, 2025
Spring Data JPA provides a convenient way to interact with relational databases through entity mappings.
The @Table annotation is used to customize how an entity class is mapped to a database table. By default, JPA uses the class name as the table name, but with @Table, developers can explicitly define table names, schemas, catalogs, and unique constraints.
Purpose
The @Table annotation is part of the Jakarta Persistence (JPA) specification and is placed above the @Entity annotation in a class.
It is mainly used when:
- The table name in the database differs from the entity class name
- A schema or catalog needs to be defined for database organization
- Unique constraints must be enforced on specific columns
Syntax:
Java
import jakarta.persistence.*;
@Entity
@Table(name = "student") // Defines custom table name
public class Student {
// Fields and methods
}
By default, JPA would create a table named Student (matching the class name). Using @Table(name = "student"), the table name is explicitly set to student.
Attributes of @Table Annotation
- name: Specifies the name of the database table (default: entity class name)
- catalog: Defines the database catalog name
- schema: Defines the schema name for the table
- uniqueConstraints: Enforces unique constraints on one or more columns
Example: Using @Table with Unique Constraint
Java
import jakarta.persistence.*;
@Entity
@Table(
name = "EMPLOYEE",
uniqueConstraints = { @UniqueConstraint(columnNames = "email") }
)
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(nullable = false, unique = true)
private String email;
}
- The table name is explicitly set to EMPLOYEE.
- A unique constraint is added on the email column, ensuring no two records share the same email address.
- @Column(unique = true) ensures column-level uniqueness, while @UniqueConstraint applies at the table level.
Implementation Steps
Step 1: Create a Spring Boot Project
Use Spring Initializr and include dependencies:
- Spring Web
- Spring Data JPA
- MySQL Driver
spring initializrClick on Generate, download, and extract the project.
spring.datasource.url=jdbc:mysql://localhost:3306/mapping
spring.datasource.username=root
spring.datasource.password=your_password
spring.jpa.hibernate.ddl-auto=update
Note: Use environment variables to protect sensitive credentials:
spring:
datasource:
username: ${DB_USER}
password: ${DB_PASS}
Step 3: Create the Entity Class
StudentInformation.java:
Java
package com.example.mapping.model;
import jakarta.persistence.*;
@Entity
@Table(name = "student")
public class StudentInformation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int rollno;
private String name;
public StudentInformation() {}
public StudentInformation(int rollno, String name) {
this.rollno = rollno;
this.name = name;
}
public int getRollno() { return rollno; }
public void setRollno(int rollno) { this.rollno = rollno; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
Step 5: Running the Application
Run the main Spring Boot application class. If everything is configured correctly, Hibernate will generate a table named student in the mapping database.
Application runningDatabase Output:
Output
Explore
Java Enterprise Edition
Multithreading
Concurrency
JDBC (Java Database Connectivity)
Java Frameworks
JUnit