@Entity Annotation in JPA Explained
Fast recall
Quick revision
Core answer
Whether you know the three entity contract requirements and why JPA needs them (reflection + proxies)
Omitting the no-arg constructor or marking the class final, breaking instantiation and lazy-loading proxies
Connect each requirement to a mechanism: no-arg constructor → reflective instantiation, non-final → runtime proxy subclassing
Interview-ready guide
Interview answer
An Order class annotated @Entity becomes part of the persistence model when the application's entity scan/persistence unit includes it. @Table can change the table mapping; otherwise provider naming rules apply. JPA instantiates loaded objects using the no-arg construction contract and populates persistent state according to field or property access. Hibernate can enhance/proxy entities for features such as lazy loading, which is why final declarations can restrict provider behavior even where an extension accepts them.
@Entity Annotation in JPA Explained
Direct Answer
@Entity marks a class as a Jakarta Persistence entity. It needs an identifier and a public or protected no-argument constructor, which may be supplied by the class. The specification requires the entity class and persistent instance variables/methods not to be final; some providers support final classes with reduced proxy/lazy behavior, but portable code follows the specification. Discovery depends on the persistence unit or Spring entity scanning.
Code Example
import jakarta.persistence.*;
import java.time.LocalDateTime;
@Entity // managed by JPA
public class Product {
@Id // mandatory primary key
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Double price;
private LocalDateTime createdAt;
protected Product() { } // no-arg ctor — required by JPA
public Product(String name, Double price) {
this.name = name;
this.price = price;
this.createdAt = LocalDateTime.now();
}
// getters/setters
}
Common Mistakes & Pitfalls
- Missing no-arg constructor — JPA throws
InstantiationExceptionwhen loading; add one (can beprotected). finalclass — JPA can't subclass it for proxies; lazy loading and tracking break.- No
@Id— startup fails with "No identifier specified for entity". - Using
javax.persistence— Spring Boot 3.2 usesjakarta.persistence.
Check your understanding
Answer these without looking back at the article.
- Why does JPA require a no-arg constructor?
- What is @Table and when do you need it?
- Why should @Entity classes not be final?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.