In Spring Boot applications, the two terms for classes are POJO and DAO. These are commonly used classes in a layered architecture application and serving different purposes in it. We need to understand the difference between POJO and DAO in Spring Boot for a better way to use them in the application development.
POJO
- Plain Old Java Object, a simple Java class with no rules to follow to create the POJO class.
- It has fields, constructors, and getter/setter methods.
- It is designed to hold data.
- It is not bound to any specific framework or inheritance hierarchy.
Characteristics of POJO
- It is a Pure Java class with no special restrictions.
- It is a Framework-independent and lightweight.
- It is used for representing structured data entities, DTOs, request bodies and etc…
Example:
public class User {
private String name;
private String email;
public User() {}
public User(String name, String email) {
this.name = name;
this.email = email;
}
// Getters and Setters
}
DAO
A DAO(Data Access Object) is a design pattern that given an abstraction for accessing and manipulating data in a database.
Characteristics of DAO
- It is responsible for CRUD operations on data sources.
- It uses JDBC, JPA, or Hibernate to communicate with databases.
- It helps decouple the business logic from persistence logic.
Example
public interface UserDao {
User findByEmail(String email);
void save(User user);
}
@Repository
public class UserDaoImpl implements UserDao {
@PersistenceContext
private EntityManager entityManager;
public User findByEmail(String email) {
return entityManager.createQuery("SELECT u FROM User u WHERE u.email = :email", User.class)
.setParameter("email", email)
.getSingleResult();
}
public void save(User user) {
entityManager.persist(user);
}
}
Difference Between POJO and DAO
Feature | POJO | DAO |
Definition | It is a Java object used to hold data | A class that handles database interactions |
Role | Data container | Data access and persistence logic |
Framework Dependency | None | Often depends on JPA, JDBC, or Hibernate |
Annotations | Typically none | Uses @Repository, @PersistenceContext, etc. |
Layer | Entity/Domain layer | Data Access layer |
Contains Logic? | There is no logic beyond getters/setters | Contains persistence logic (queries, updates, etc.) |
Used For | It is used for representing structured data | Performing operations like save, update and delete |
When to Use POJO vs DAO
Use POJO when
- To store and retrieve data.
- Creating classes like domain models, DTOs, or API request/response objects.
Use DAO when
- To fetch or update data in a database.
- To use JPA, Hibernate, or JDBC for persistence.
- To separate persistence logic from business logic.
How They Work Together
In a typical Spring Boot application:
Controller → Service → DAO → Database
↓
POJO (Entity)
- The POJO User class represents the data structure.
- DAO performs operations on POJOs using the database like save, find and etc….
Conclusion
In summary:
- POJO is a data holder — used to represent objects like User, Product, etc.
- DAO is a persistence layer component — used to perform database operations on POJOs.
- They work together: DAO retrieves and stores POJOs, and POJOs carry the data.