Separating DTOs and entities helps keep your app organized and safe. It stops your database details from mixing with how data moves in and out of your app.
0
0
DTO vs entity separation benefit in Spring Boot
Introduction
When you want to send only specific data to users without exposing all database fields.
When you need to change how data is shown without changing the database structure.
When you want to protect your database from accidental changes by users.
When you want to keep your code clean by separating data storage from data transfer.
When you want to validate or format data before sending or saving it.
Syntax
Spring Boot
public class UserEntity { private Long id; private String username; private String password; // getters and setters } public class UserDTO { private String username; // getters and setters }
Entity classes map directly to database tables.
DTO classes are simple objects used to transfer data between layers or systems.
Examples
The DTO hides supplier info when sending product data to clients.
Spring Boot
public class ProductEntity { private Long id; private String name; private double price; private String supplierInfo; // getters and setters } public class ProductDTO { private String name; private double price; // getters and setters }
Internal notes are kept private in the entity and not sent in the DTO.
Spring Boot
public class OrderEntity { private Long id; private java.util.Date orderDate; private String customerName; private String internalNotes; // getters and setters } public class OrderDTO { private java.util.Date orderDate; private String customerName; // getters and setters }
Sample Program
This example shows how the UserEntity holds all data including password, but the UserDTO only carries the username. This keeps sensitive info safe when sending data out.
Spring Boot
package com.example.demo.dto; public class UserDTO { private String username; public UserDTO() {} public UserDTO(String username) { this.username = username; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } } package com.example.demo.entity; public class UserEntity { private Long id; private String username; private String password; public UserEntity() {} public UserEntity(Long id, String username, String password) { this.id = id; this.username = username; this.password = password; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } package com.example.demo; import com.example.demo.dto.UserDTO; import com.example.demo.entity.UserEntity; public class Main { public static void main(String[] args) { // Create an entity with sensitive data UserEntity userEntity = new UserEntity(1L, "alice", "secret123"); // Convert entity to DTO to send only safe data UserDTO userDTO = new UserDTO(userEntity.getUsername()); // Print DTO data System.out.println("UserDTO username: " + userDTO.getUsername()); } }
OutputSuccess
Important Notes
Always keep sensitive data like passwords only in entities, never in DTOs.
DTOs can help reduce data size sent over the network by excluding unnecessary fields.
Mapping between entities and DTOs can be done manually or with libraries like MapStruct.
Summary
Entities represent how data is stored in the database.
DTOs represent how data is sent or received outside the database.
Separating them keeps your app safer and easier to maintain.