Which annotation is used to specify the primary key of an entity in JPA?

Java MCQ: Which annotation is used to specify the primary key of an entity in JPA?

a) @PrimaryKey
b) @Id
c) @Key
d) @GeneratedValue

Answer:

b) @Id

Explanation:

The @Id annotation is used to specify the primary key of an entity in JPA. The field or property annotated with @Id represents the unique identifier for each entity instance, corresponding to the primary key column in the database table.

Here’s an example:

@Entity
public class Employee {
    @Id
    private Long id;

    private String name;

    // Getters and setters
}

In this example, the id field is marked with @Id, indicating that it is the primary key of the Employee entity.

The primary key is essential for uniquely identifying and retrieving entity instances in JPA.

Reference links:

https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top