InterviewVault
Welcome back, Sujit Kumar Mishra
Admin
SK Mishra
Revision Mode
Document technical questions and best-practice answers.
Entity vs DTO
- What is the difference between an Entity class and a DTO (Data Transfer Object)?
Entity:
An Entity class represents the actual data stored in the database. It usually includes all fields and relationships needed for persistence.
DTO (Data Transfer Object):
A DTO is used to transfer data between processes, layers, or systems. It contains only the data needed for a specific operation, and may exclude extra fields or logic.
Easy way to remember:
Entity = Database object (full data, persistent)
DTO = Data carrier (only what’s needed, for transfer)
Entity Example:
public class User {
private Long id;
private String name;
private String email;
private String password;
// getters and setters
}
DTO Example:
public class UserDTO {
private String name;
private String email;
// getters and setters
}
Summary:
1: Entity has all fields, including database-related ones (like id, password).
2: DTO has only the fields needed for transfer (like name, email).