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.
DTO vs entity separation benefit in Spring Boot
Start learning this pattern below
Jump into concepts and practice - no test required
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.
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 }
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 }
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.
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()); } }
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.
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.
Practice
Solution
Step 1: Understand the role of entities
Entities represent the database structure and are tightly linked to how data is stored.Step 2: Understand the role of DTOs
DTOs are used to transfer data safely between layers or systems, hiding internal details.Final Answer:
It keeps the database structure hidden and improves security. -> Option CQuick Check:
DTOs separate data transfer from entities = A [OK]
- Thinking DTOs speed up database calls
- Believing entities should be exposed directly
- Confusing DTOs with database tables
Solution
Step 1: Review DTO class options
DTOs are simple data carriers. Java records provide a concise way to define immutable DTOs.Step 2: Identify the correct syntax
public record UserDTO(String name) {} uses a record, which is modern and recommended for DTOs in Java 17+.Final Answer:
public record UserDTO(String name) {} -> Option BQuick Check:
Use records for simple DTOs = D [OK]
- Using interfaces without methods for DTOs
- Using enums instead of classes or records
- Not providing getters for DTO fields
record UserDTO(String name) {}
class UserEntity { String name; UserEntity(String name) { this.name = name; } }
UserEntity entity = new UserEntity("Alice");
UserDTO dto = new UserDTO(entity.name);
System.out.println(dto.name());Solution
Step 1: Understand entity to DTO conversion
The entity has a name "Alice" which is passed to the DTO constructor.Step 2: Check the output of dto.name()
Since dto stores "Alice", printing dto.name() outputs "Alice".Final Answer:
Alice -> Option AQuick Check:
Entity name passed to DTO = Alice [OK]
- Assuming dto.name() returns null
- Confusing record syntax causing errors
- Expecting entity and DTO to be the same object
public class UserEntity {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String toJson() { return "{\"name\":\"" + name + "\"}"; }
}Solution
Step 1: Analyze entity responsibilities
Entities should focus on data storage and mapping, not formatting or presentation.Step 2: Identify separation violation
toJson mixes data with presentation logic, which belongs in DTO or service layers.Final Answer:
Entity class should not handle JSON formatting. -> Option DQuick Check:
Keep entity and presentation separate = A [OK]
- Allowing entities to format output
- Making entity fields public
- Confusing DTO and entity roles
Solution
Step 1: Recognize sensitive data risks
Entities contain all data, including sensitive info like passwords, which should not be exposed.Step 2: Understand DTO role in security
DTOs can include only safe fields, preventing accidental exposure in API responses.Step 3: Consider maintainability benefits
Separating DTOs allows easier changes to API without affecting database structure.Final Answer:
By exposing only necessary fields and hiding sensitive data from API responses. -> Option AQuick Check:
DTOs protect sensitive data and ease maintenance = B [OK]
- Exposing all entity fields directly
- Skipping validation by merging DTO and entity
- Removing service layers causing tight coupling
