The @Entity annotation marks a class as a database table. It helps Spring Boot know which classes to save and load from the database.
JPA entity with @Entity annotation in Spring Boot
Start learning this pattern below
Jump into concepts and practice - no test required
@Entity
public class ClassName {
@Id
private Long id;
// other fields
}The class must have a unique identifier marked with @Id.
The class name usually matches the table name but can be customized.
@Entity
public class User {
@Id
private Long id;
private String name;
private String email;
}@Entity
public class Product {
@Id
private Long productId;
private String productName;
private double price;
}This is a complete JPA entity class named Book. It has an @Entity annotation to mark it as a database table. The id field is the unique identifier marked with @Id. It includes constructors, getters, setters, and a toString method for easy printing.
import jakarta.persistence.Entity; import jakarta.persistence.Id; @Entity public class Book { @Id private Long id; private String title; private String author; public Book() {} public Book(Long id, String title, String author) { this.id = id; this.title = title; this.author = author; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } @Override public String toString() { return "Book{id=" + id + ", title='" + title + "', author='" + author + "'}"; } }
Every entity needs a field annotated with @Id to serve as the primary key.
Use a no-argument constructor so frameworks can create instances easily.
Fields without annotations are mapped to columns with the same name by default.
@Entity marks a class as a database table.
Each entity needs a unique @Id field.
Entities help connect Java objects with database records automatically.
Practice
@Entity annotation in a Spring Boot application?Solution
Step 1: Understand the role of
The@Entity@Entityannotation tells Spring Boot and JPA that this class represents a table in the database.Step 2: Differentiate from other annotations
Other annotations like@RestControlleror@Serviceserve different purposes unrelated to database tables.Final Answer:
To mark a class as a database table for JPA -> Option AQuick Check:
@Entitymarks database tables [OK]
@Entity means database table class [OK]- Confusing
@Entitywith@Service - Thinking
@Entitycreates REST endpoints - Assuming
@Entityconfigures app settings
Solution
Step 1: Check for
The class must have@Entityannotation@Entityto be recognized as a JPA entity.Step 2: Verify presence of
Every entity needs a unique identifier marked with@Idon a field@Idto map the primary key.Final Answer:
@Entity public class User { @Id private Long id; } -> Option CQuick Check:
Entity + Id field = correct syntax [OK]
@Entity and @Id on ID field [OK]- Missing
@Entityannotation - Forgetting
@Idon the ID field - Using
@Serviceinstead of@Entity
@Entity
public class Product {
@Id
private Long id;
private String name;
public Product() {}
public Product(Long id, String name) {
this.id = id;
this.name = name;
}
public String getName() { return name; }
}What will happen if you save a
Product with id=1 and name="Book" using JPA repository and then retrieve it?Solution
Step 1: Understand JPA entity saving and retrieval
JPA uses the@Entityclass and its fields to save and load data. The constructor and getter allow access to fields.Step 2: Check if missing setter affects retrieval
JPA can set fields via reflection even without setters, sonamewill be loaded correctly.Final Answer:
You get a Product object with id=1 and name="Book" -> Option BQuick Check:
JPA loads fields even without setters [OK]
- Assuming missing setter causes runtime error
- Thinking @Column is mandatory for saving
- Believing name will be null without setter
@Entity
public class Customer {
private Long id;
private String email;
public Long getId() { return id; }
public String getEmail() { return email; }
}Solution
Step 1: Check for
The class has@Entityannotation@Entity, so it is recognized as an entity.Step 2: Verify presence of
The@Idannotationidfield lacks@Id, so JPA cannot identify the primary key, causing errors.Final Answer:
Missing@Idannotation on the id field -> Option DQuick Check:
Every entity needs@Idon primary key [OK]
@Id on the primary key field [OK]- Assuming default constructor is mandatory (JPA provides one)
- Thinking fields must be public
- Ignoring missing
@Idannotation
Order with a composite primary key made of orderId and productId. Which approach correctly applies the @Entity annotation and primary key setup?Solution
Step 1: Understand composite keys in JPA
JPA requires a separate class annotated with@Embeddableto represent composite keys.Step 2: Use
The entity class uses@EmbeddedIdin the entity@EmbeddedIdto include the composite key class as its primary key.Final Answer:
Use@Entityon Order and create a separate @Embeddable class for the composite key with @EmbeddedId in Order -> Option AQuick Check:
Composite key needs @Embeddable + @EmbeddedId [OK]
- Marking multiple fields with @Id without composite key class
- Ignoring one key field in composite key
- Trying to configure keys in properties file
