What is the @Entity annotation and JPA basics?
Why Interviewers Ask This
This is a classic screening question for Spring Boot roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.
Answer
JPA (Java Persistence API) is the standard specification for ORM (Object-Relational Mapping) in Java. Hibernate is the most common implementation. Core annotations: @Entity — marks a class as a JPA entity (maps to a DB table); @Table(name = "users") — specify table name (optional if same as class name); @Id — marks the primary key field; @GeneratedValue(strategy = GenerationType.IDENTITY) — auto-generate primary key (IDENTITY uses DB auto-increment, SEQUENCE uses DB sequence, AUTO lets JPA decide); @Column(name = "email_address", nullable = false, unique = true, length = 255) — map to a column with constraints; @Transient — field NOT persisted to DB; @Enumerated(EnumType.STRING) — store enum as string (not ordinal). Example entity: @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false) private String name; @Column(unique = true, nullable = false) private String email; @Enumerated(EnumType.STRING) private UserStatus status = UserStatus.ACTIVE; @CreatedDate @Column(updatable = false) private LocalDateTime createdAt; @LastModifiedDate private LocalDateTime updatedAt; }. Relationships: @OneToOne, @OneToMany, @ManyToOne, @ManyToMany. Cascade: @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) — operations propagate to children. Fetch types: LAZY (default for @OneToMany, @ManyToMany) vs EAGER (default for @ManyToOne, @OneToOne).
Pro Tip
This topic has Spring Boot-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.
Previous
What is @Valid and Bean Validation in Spring Boot?
Next
What is Spring Boot exception handling?