Last Updated : 28 Mar, 2025
Spring Data JPA is a powerful framework that simplifies database interactions in Spring Boot applications. The @Table annotation in JPA (Java Persistence API) is used to specify the table name in the database and ensure proper mapping between Java entities and database tables. This is especially useful when:
In this article, we will explore how to use the @Table annotation in Spring Data JPA with an example.
@Table AnnotationThe @Table annotation in JPA is used to define the database table mapping for an entity. It allows customization of:
Syntax:
import jakarta.persistence.*;
@Entity
@Table(name = "student") // Custom table name
public class Student {
// Fields and methods
}
In this example, the table is explicitly named "student" instead of relying on the default entity name.
Attributes of @Table AnnotationThe @Table annotation provides the following attributes:
Attribute
Description
name
It defines the table name (default: entity class name).
catalog
It specifies the database catalog.
schema
It defines the database schema.
uniqueConstraints
It enforces unique constraints on specific columns.
Example of @Table with Unique Constraint Java
@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;
}
In this example, the email field must be unique across all rows in the EMPLOYEE table.
Step-by-Step Implementation Step 1: Create a Spring Boot ProjectClick on Generate, download, and extract the project.
Step 2: Import the Project into Your IDEpom.xml
is recognized and dependencies are downloaded.Adding the necessary properties in the application.properties file. (mapping is the database name)
spring.datasource.url=jdbc:mysql://localhost:3306/mapping
spring.datasource.username=root
spring.datasource.password=your_password
spring.jpa.hibernate.ddl-auto=update
Best Practice: Avoid hardcoding credentials. Use environment variables:
Step 4: Create the Entity Class Project Structure:spring:
datasource:
username: ${DB_USER}
password: ${DB_PASS}
Create a model folder in the project folder and make a StudentInformation class.
StudentInformation.java:
Java
package com.example.mapping.models;
import jakarta.persistence.*;
@Entity
@Table(name = "student") // Custom table name
public class StudentInformation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int rollno;
private String name;
// Default constructor
public StudentInformation() {}
// Parameterized constructor
public StudentInformation(int rollno, String name) {
this.rollno = rollno;
this.name = name;
}
// Getters and Setters
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 application class.
Database Output:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4