What is the @Id annotation used for?

@Id is a JPA annotation that marks a field or property on an entity class as its primary key, telling the persistence provider which value uniquely identifies each row and how to track and manage that entity's identity.

Key Points

Example

An Employee entity's employeeId field is annotated with @Id so JPA knows that column is the primary key used to look up, update, and uniquely identify each employee record.

@Entity
public class Employee {

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

    private String name;
}

Interview Tip

A concise interview answer is

@Id marks the field that JPA treats as the entity's primary key, which it uses to track identity, decide whether save() inserts or updates, and generate the underlying SQL. I usually pair it with @GeneratedValue so the database or provider auto-generates the key value on insert.

Last updated: 11 November 2026

Previous question: What is the difference between FetchType.EAGER and FetchType.LAZY?

Next question: How do you create a composite primary key in Spring Data JPA?

More Spring Data JPA interview questions

All Spring Data JPA questions