In Spring Boot, DAO, DTO, and POJO are commonly used in the project’s layered architecture and codebases. While they might sound similar. However, they have very different purposes and roles to each other in the application development process.In this topic, we will learn to compare DAO vs DTO vs POJO, with their examples and best practices to help us structure our Spring Boot application for better readability, maintainability, and scalability.
DAO(Data Access Object)
A DAO is a design pattern that provides an abstract interface to some type of database or other persistence mechanism. DAOs manage the data retrieval, insertion, deletion, and update operations.
Key Features
- Encapsulates all access to the data source.
- Typically interacts with JPA, Hibernate, or JDBC.
- Contains SQL or JPQL logic.
- Annotated with @Repository.
Example
public interface UserDao {
User findById(Long id);
void save(User user);
}
@Repository
public class UserDaoImpl implements UserDao {
@PersistenceContext
private EntityManager entityManager;
public User findById(Long id) {
return entityManager.find(User.class, id);
}
public void save(User user) {
entityManager.persist(user);
}
}
DTO (Data Transfer Object)
A DTO is a simple Java object used to transfer data between layers of an application, especially from the backend to the frontend via APIs. DTOs are not persisted in the database and contain only the data required for a particular view or use case.
Key Features
- Encapsulates data for transfer between layers (Service ↔ Controller).
- Removes unnecessary fields from entities in API responses.
- Helps prevent overexposing the internal structure (e.g., password, IDs).
- Lightweight and fast.
Example
public class UserDTO {
private String username;
private String email;
public UserDTO(String username, String email) {
this.username = username;
this.email = email;
}
// Getters and Setters
}
POJO (Plain Old Java Object)
A POJO is any simple Java class that doesn’t extend or implement any framework-specific interfaces or classes. It’s the foundation for creating DTOs, Entities, and even Models.
Key Features
- Does not depend on any external framework (e.g., JPA, Spring).
- Typically includes fields, constructors, getters, and setters.
- Can be used to represent structured data.
- May be used as a base class for DTOs and entities.
Example
public class UserPOJO {
private String name;
private int age;
// Getters and Setters
}
DAO vs DTO vs POJO
Feature | DAO (Data Access Object) | DTO (Data Transfer Object) | POJO (Plain Old Java Object) |
Role | Manages data access (database interaction) | Transfers data between application layers | Basic data structure without business logic |
Persistence | Yes, interacts with the database | No | No |
Contains Logic | Yes (database access logic) | No | No |
Annotations | @Repository (or manual DAO class) | None (may use @Data, @Getter, etc.) | None |
Used In | Service layer to communicate with DB | Controller ↔ Service communication | Can be used anywhere as a value object |
Depends On | JPA, Hibernate, JDBC | Core Java (may use Lombok for brevity) | Pure Java only |
When to Use DAO, DTO, and POJO in Spring Boot
Use DAO When
- We need to create a data access layer manually.
- We are using custom queries, or your project doesn’t use Spring Data.
- We want complete control over SQL, sessions, and transactions.
Use DTO When
- We are exposing APIs and want to control what data is shared.
- We want to avoid overfetching or underfetching data.
- We need lightweight request/response objects.
Use POJO When
- We want a simple Java object to carry data or hold intermediate values.
- We are building DTOs, entities, or models.
- We want a framework-independent data structure.
How They Work Together
Here’s a quick example of how these terms interact in a real-world Spring Boot application:
Client ↔ Controller ↔ Service ↔ DAO ↔ Database
↑ ↓
DTO Entity
↑ ↓
POJO POJO (Entity)
Example Flow
- DAO: Retrieves a UserEntity from the database.
- Service: Converts UserEntity to UserDTO.
- Controller: Returns UserDTO as API response.
- POJO: Used in defining UserDTO or UserEntity (basic field holder).
Practical Example
UserEntity.java (POJO + JPA Entity)
@Entity
public class UserEntity {
@Id
private Long id;
private String username;
private String email;
private String password;
}
UserDTO.java (POJO for API)
public class UserDTO {
private String username;
private String email;
}
UserDao.java
public interface UserDao {
UserEntity findByUsername(String username);
}
UserService.java
@Service
public class UserService {
@Autowired
private UserDao userDao;
public UserDTO getUser(String username) {
UserEntity user = userDao.findByUsername(username);
return new UserDTO(user.getUsername(), user.getEmail());
}
}
Conclusion
In Spring Boot development:
- DAO handles database access logic—it’s the bridge between our code and the DB.
- DTO is used to transfer data—especially for external-facing APIs.
- POJO is a Plan of Java object used to structure data and is foundational for entities and DTOs.
Understanding how these terms differ helps us to write better code, improve performance, and maintain separation of concerns in our application.