Difference Between POJO and DAO in Spring Boot

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.

Difference Between POJO and DAO in Spring Boot

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

FeaturePOJODAO 
DefinitionIt is a Java object used to hold dataA class that handles database interactions
RoleData containerData access and persistence logic
Framework DependencyNoneOften depends on JPA, JDBC, or Hibernate
AnnotationsTypically noneUses @Repository, @PersistenceContext, etc.
LayerEntity/Domain layerData Access layer
Contains Logic?There is no logic beyond getters/settersContains persistence logic (queries, updates, etc.)
Used ForIt is used for representing structured dataPerforming 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.

Leave a Comment