DEV Community

Azna Aroos
Azna Aroos

Posted on

Entity vs DTO vs Model — Stop Using These Terms Interchangeably

Do you ever confuse yourself with the terms Entity, Model, and DTO? Sometimes you might hear your lecturer say that you can call an Entity a Model, but is that really true?
They may sound similar, but they are actually quite different. Let's understand what they really are and what they actually do.

Entity

An Entity represents a database table using annotations like @entity. Think of it as a real table in your database.
Example: Student Table

@Entity
@Table(name = "students")
public class StudentEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String email;
    private String password;
}
Enter fullscreen mode Exit fullscreen mode

An Entity is used by JPA/Hibernate. It is tightly coupled to the database and typically contains all fields, including sensitive ones such as passwords.

DTO (Data Transfer Object)

A DTO is used to transfer data between layers.

It is not directly connected to the database. It is a safer, filtered version of your data that you send to the frontend.

public class StudentDTO {
    private Integer id;
    private String name;
}

Enter fullscreen mode Exit fullscreen mode

Model

This is where many people get confused. In Spring Boot, Model is often used as a general term for business objects. In MVC architecture, a Model is specifically used to pass data to views. Sometimes developers use "Model" in place of "Entity," so its meaning depends on the context.
In Spring MVC, a Model is simply used to pass data to a view template like Thymeleaf.

@GetMapping("/student")
public String getStudent(Model model) {
    model.addAttribute("name", "Aqueel");
    return "StudentPage";
}
Enter fullscreen mode Exit fullscreen mode

A Simple Analogy — The Toy Shop
Imagine you own a toy shop.
Think of the Entity as your back storage room. It contains everything:

  • All toys
  • Their cost price
  • Supplier details
  • Secret discount rules

That's your Entity — directly connected to the database.
When customers walk in, you don't take them to the storage room. You only show them:

  • Toy name
  • Selling price
  • Color

You hide the supplier info and purchase price from them. That filtered view is your DTO — a safe version of the data.
The Model is like the receipt at the counter. When someone buys a toy, it shows:

  • Toy name
  • Quantity
  • Total price

Why Not Just Use Entity Everywhere?

  1. - Security risk — sensitive fields like passwords get exposed.
  2. - Over-fetching — you return more data than the client actually needs.
  3. - Tight coupling — your database structure becomes directly tied to your API.
  4. - Hard to scale — changes to the database will break your API responses.

The Golden Rule:

Use Entity to talk to your database, DTO to talk to the outside world, and Model to talk to your views.

Think of it this way,

Entity = what lives in your database 🗄️
DTO = what you show to the world 🌐
Model = what you pass to your UI 🖥️

Never expose your Entity directly to the frontend. Always filter your data through a DTO. The extra effort keeps your application secure, clean, and scalable.

Found this helpful? ♻️ Repost to help other developers.
Follow for more tips! 🚀

SpringBoot #Java #Backend #WebDevelopment #Programming #DTO #JPA #SoftwareEngineering #Developer

Top comments (0)