Open In App
Related Articles

Spring Data JPA – @Table Annotation

Improve Article
Save Article
Like Article

Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. In this article, we will discuss how to add table names to Spring Boot Project. @Table(), the JPA annotation is used for adding the table name in the particular MySQL database. 




public class Student {  




Name: The name of the table. It is an optional attribute.

Use of @Table annotation in JPA

The @Table annotation allows you to specify the details of the table that will be used to persist the entity in the database. The @Table annotation provides four attributes, allowing you to override the name of the table, its catalog, and its schema, and enforce unique constraints on columns in the table. For now, we are using just the table name which is EMPLOYEE.

@Table(name = "EMPLOYEE")
public class Employee {
   @Id @GeneratedValue
   @Column(name = "id")
   private int id;


Step 1: Go to this link. Fill in the details as per the requirements. For this application:

Project: Maven
Language: Java
Spring Boot: 2.5.6
Packaging: JAR
Java: 11
Dependencies: Spring Web,Spring Data JPA, MySql Driver

Click on Generate which will download the starter project.

Step 2: Extract the zip file. Now open a suitable IDE and then go to File > New > Project from existing sources > Mapping and select pom.xml. Click on import changes on prompt and wait for the project to sync as pictorially depicted below as follows:

Step 3: Adding the necessary properties in the file. (mapping is the database name)


Step 4: Create a model folder in the project folder and make a StudentInformation class.



package com.example.Mapping.Models;
import java.util.*;
import javax.persistence.*;
// Adding the table name
@Table(name = "Student")
public class StudentInformation {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int rollno;
    private String name;
    public int getRollno() { return rollno; }
    public StudentInformation() {}
    public StudentInformation(int rollno, String name)
        this.rollno = rollno; = name;
    public void setRollno(int rollno)
        this.rollno = rollno;
    public String getName() { return name; }
    public void setName(String name) { = name; }

Run the main application:

Database output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 29 Dec, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials