Open In App
Related Articles

Spring Data JPA – @Id Annotation

Improve Article
Save Article
Like Article

Spring Boot is built on the top of the spring and contains all the features of spring. Spring also provides JPA and hibernate to increase the data manipulation efficiency between the spring application and the database. In very simple terms we can say JPA (Java persistence API) is like an interface and the hibernate is the implementation of the methods of the interface Like how insertion will be down is already defined with the help of hibernate. In this article, we will discuss how to make a specific variable of the class primary key in the Spring  Application. @Id annotation is the JPA is used for making specific variable primary key.

Use of @Id Annotation in JPA

The @Id annotation is inherited from javax.persistence.Id, indicating the member field below is the primary key of the current entity. Hence your Hibernate and spring framework as well as you can do some reflect works based on this annotation. 


Spring Initializr is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also provides various different features for the projects expressed in a metadata model. This model allows us to configure the list of dependencies that are supported by JVM. Here, we will create the structure of an application using a spring initializer.

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: Go to src->main->java->com->example->Mapping and create a file

Project Structure:



@Table(name = "Student")
public class StudentInformation {
    // Making roll number primary key
    @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:

Terminal Output:

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