In Spring Boot, Model, Entity, and DTO (Data Transfer Object) are used commonly but they have different purposes to use in application architecture. Understanding the Model vs Entity vs DTO helps in designing a clean, maintainable, and scalable Spring Boot application. This guide explains:
- What are Model, Entity, and DTO?
- Key differences between them.
- Best practices and when to use each in our Spring Boot project.
What is Entity in Spring Boot?
An Entity represents a database table and is mapped using JPA (Java Persistence API). Entities are persisted in the database and used for CRUD operations.
Key Features of Entity
✔ Mapped to a database table using JPA.
✔ Uses @Entity, @Table, @Id, and other JPA annotations.
✔ Can be used in Spring Data JPA repositories.
✔ Supports relational mappings (@OneToMany, @ManyToOne).
Example: UserEntity (JPA Entity for Database)
import javax.persistence.*;
@Entity
@Table(name = "users")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String email;
// Getters and Setters
}
Entity is Best for
✔ Database storage and persistence.
✔ Performing CRUD operations using JPA repositories.
✔ Defining relationships between tables (@OneToMany, @ManyToOne).
What is a Model in Spring Boot?
A Model is a generic term that represents data structures in the application. It can refer to DTOs, View Models, or Business Logic Models. A Model does not always persist in the database.
Key Features of Model
✔ Used for DTOs, API responses, and business logic models.
✔ May or may not have JPA annotations.
✔ Helps in separating concerns between layers (Controller, Service).
Example: UserModel (A Simple Java Class for Business Logic)
public class UserModel {
private String username;
private String email;
private boolean isActive;
// Constructor
public UserModel(String username, String email, boolean isActive) {
this.username = username;
this.email = email;
this.isActive = isActive;
}
// Getters and Setters
}
Model is Best for
✔ Business logic (non-persistent classes).
✔ Data transfer within the application.
✔ Complex computed values that should not be stored in the database.
What is DTO in the Spring Boot?
The DTO is a Data Transfer Object to transfer data between layers (Controller ⇄ Service ⇄ API).DTOs help in hiding internal database structures and optimizing API responses.
Key Features of DTO
✔ Used for request and response handling in APIs.
✔ Prevents direct exposure of entities to clients.
✔ Can contain transformed or aggregated data.
✔ Reduces unnecessary database fields in API responses.
Example: UserDTO (Used in API Responses)
public class UserDTO {
private String username;
private String email;
public UserDTO(String username, String email) {
this.username = username;
this.email = email;
}
// Getters and Setters
}
DTO is Best for
✔ API request and response models.
✔ Reducing unnecessary fields in API responses.
✔ Preventing direct exposure of database entities.
✔ Improving security and performance in APIs.
Model VS Entity VS DTO
Feature | Entity | Model | DTO |
Purpose | Represents a database table (persistent). | Represents business logic or computations. | Represents API request/response objects. |
Persistence | Stored in the database using JPA. | Not stored in the database. | Not stored in the database. |
Annotations | Use JPA annotations (@Entity, @Table). | No strict annotations. | No JPA annotations. |
Use Case | Used for CRUD operations via JPA repositories. | Used for business logic computations. | Used for data transfer between layers. |
Data Scope | Represents complete data from the database. | Can contain computed, derived, or extra data. | Contains only required fields. |
Modification | Changes affect database schema. | Changes do not affect the database. | Changes do not affect the database. |
When to Use Model, Entity, and DTO in Spring Boot?
Use Entity When
✔ You need to store data persistently in the database.
✔ You require JPA annotations (@Entity, @Table, @Id).
✔ You need Spring Data JPA repository methods to fetch data.
✔ You are implementing relational mappings (@OneToMany, @ManyToOne).
Example: UserEntity Stored in the Database
@Entity
@Table(name = "users")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String email;
}
Use Model When
✔ You need a custom class for business logic computations.
✔ You want to avoid exposing Entity structure directly in APIs.
✔ You need a View Model to format data before sending it to the UI.
✔ You require data transformation between layers.
Example: UserModel for Business Logic
public class UserModel {
private String username;
private String email;
private boolean isActive;
}
Use DTO When
✔ We need an API request or response object.
✔ We want to exclude database-specific fields in API responses.
✔ We need a lightweight object for REST API communication.
✔ We need to transform data before sending it to clients.
Example: UserDTO for API Response
public class UserDTO {
private String username;
private String email;
public UserDTO(String username, String email) {
this.username = username;
this.email = email;
}
}
How to Convert Between Entity, Model, and DTO?
Since Entities are used for persistence and DTOs are used for API responses, you often need to convert between them.
Manual Mapping: Convert Entity to DTO
public UserDTO convertToDTO(UserEntity user) {
return new UserDTO(user.getUsername(), user.getEmail());
}
Using ModelMapper for Automatic Conversion
import org.modelmapper.ModelMapper;
public UserDTO convertToDTO(UserEntity user) {
ModelMapper modelMapper = new ModelMapper();
return modelMapper.map(user, UserDTO.class);
}
Conclusion
In Spring Boot,
✔ Use Entity for database persistence – Entities represent database tables and are used with JPA repositories.
✔ Use Model for business logic – Models help in data representation and business logic processing.
✔ Use DTO for API responses – DTOs keep API responses lightweight and secure.
By following this approach, we can create clean, maintainable, and scalable Spring Boot applications.