In JPA, what do @Entity and @Id mean?
Options
@Entitymarks a class mapped to a table;@Idmarks the primary key (Correct option)@Entityis a REST controller;@Idis a URL parameter@Entitycreates a bean;@Idis the id of a thread- Both only change how the class is written to the logs
Answer
@Entity marks a Java class that maps to a table; @Id marks the field that is the primary key
Why
JPA (Jakarta Persistence API) is the standard for mapping Java objects to relational tables (ORM). Hibernate is the most popular implementation.
@Entity says: instances of this class are rows of a table. @Id marks the primary key field. @GeneratedValue lets the database generate it, @Column customizes a column.
Every entity needs a no-argument constructor (can be protected) because the provider creates objects by reflection.
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String email;
}Remember
@Entity = table row object. @Id = primary key.