Spring Data JPA @Id Annotation (original) (raw)

Last Updated : 9 Apr, 2026

The @Id annotation is used to define the primary key of a JPA entity, uniquely identifying each record in the database. It works with @GeneratedValue to automatically generate IDs.

Syntax

import jakarta.persistence.*;

@Entity
public class ExampleEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}

Here:

Common @GeneratedValue Strategies

Steps to Implement @Id Annotation

We’ll now create a simple Spring Boot application to demonstrate how the @Id annotation works with Spring Data JPA and MySQL.

Step 1: Create a New Spring Boot Project

Go to Spring Initializr and configure the project as follows:

Spring-Initializr

spring initializr

Click Generate, download the ZIP file, and import it into your IDE (e.g., IntelliJ IDEA or Eclipse).

Step 2: Configure the Database Connection

Add the following properties in src/main/resources/application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/mapping
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=true

Step 3: Create the Entity Class

Create a new file named StudentInformation.java under src/main/java/com/example/mapping/.

Java `

package com.example.mapping;

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; }

}

`

**Explanation:

**Note: Without @Id, Hibernate cannot map the entity properly to a table row.

Step 4: Create the Repository Interface

Create StudentRepository.java in the same package:

Java `

package com.example.mapping;

import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;

@Repository public interface StudentRepository extends JpaRepository<StudentInformation, Integer> { }

`

**Explanation:

Step 5: Main Application Class

No changes in main application class.

Java `

package com.example.mapping;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication public class MappingApplication { public static void main(String[] args) { SpringApplication.run(MappingApplication.class, args); } }

`

**Explanation:

Running the Application

**Console Output:

Console-Output

Console Output

Verifying the Table in MySQL

1. Start the MySQL server.

2. Log in using the terminal:

mysql -u root -p

3. Switch to the database:

USE mapping;

4. List tables:

SHOW TABLES;

5. View table structure:

DESCRIBE Student;

Database-Output

Output