Logo InterviewVault

Welcome back, Sujit Kumar Mishra

SKM

Revision Mode

Document technical questions and best-practice answers.

Cancel

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).

Ready for commit