Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Spring Data JPA – @Id Annotation

  • Last Updated : 29 Dec, 2021

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. 

Example

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 application.properties file. (mapping is the database name)

spring.datasource.username=root
spring.datasource.password=Aayush
spring.datasource.url=jdbc:mysql://localhost:3306/mapping
spring.jpa.hibernate.ddl-auto=update

Step 4: Go to src->main->java->com->example->Mapping and create a file StudentInformation.java

Project Structure:

StudentInformation:

Java




@Entity
@Table(name = "Student")
public class StudentInformation {
    // Making roll number primary key
    @Id
    @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;
        this.name = name;
    }
  
    public void setRollno(int rollno)
    {
  
        this.rollno = rollno;
    }
  
    public String getName() { return name; }
  
    public void setName(String name) { this.name = name; }
}

Run the main application:

Terminal Output:

Database Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!