In Spring Boot, both DAO (Data Access Object) and Repository are layers used to communicate with the database. However, they serve similar uses—data access and persistence. But they belong to different eras and philosophies of application development. This guide breaks down the difference between DAO and Repository, explains their use cases, and offers best practices in modern Spring Boot applications using Spring Data JPA.
What is the DAO?
It is a design pattern used to abstract and encapsulate all communications with a data source and also provides us with methods to perform CRUD operations, often using JDBC or ORM tools like Hibernate.
Key Features of DAO
✔ Encapsulates data access logic
✔ Typically uses manual queries or Hibernate sessions
✔ Used before Spring Data JPA became mainstream
✔ Can be implemented using custom interfaces and classes
Example: DAO Interface and Implementation
public interface UserDao {
void save(User user);
User findById(Long id);
}
@Repository
public class UserDaoImpl implements UserDao {
@PersistenceContext
private EntityManager entityManager;
public void save(User user) {
entityManager.persist(user);
}
public User findById(Long id) {
return entityManager.find(User.class, id);
}
}
What is Repository?
It is a Spring Data abstraction built on top of DAO concepts. It automatically provides implementations for data access using Spring Data JPA and reduces boilerplate code by supporting derived queries.
Key Features of Repository
✔ Part of Spring Data JPA
✔ Uses interfaces only—no manual implementation required
✔ Supports method naming conventions to generate queries
✔ Enables JPQL, native SQL, pagination, and sorting
✔ Less boilerplate, more concise
Example: Spring Data Repository
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
- You don’t need to write implementation code; Spring provides it at runtime.
Key Difference Between DAO and Repository
Feature | DAO (Data Access Object) | Repository (Spring Data JPA) |
Concept Origin | Design Pattern (classic, generic) | Spring Framework abstraction |
Code Structure | Interface + Implementation | Interface only |
Boilerplate Code | Requires manual implementation | Automatically implemented by Spring |
Ease of Use | Moderate (requires more code) | High (minimal code required) |
Query Support | Manual with EntityManager or Hibernate | Method names, JPQL, @Query, native SQL |
Annotations | @Repository, @Transactional, etc. | @Repository or none (detected automatically) |
Use Case | Legacy apps or custom persistence logic | Modern Spring Boot apps with Spring Data JPA |
Flexibility | More flexible but verbose | Less flexible, more declarative |
When to Use DAO
- You need custom, complex queries not supported by Spring Data
- You’re working on a legacy application using Hibernate or JDBC
- You want full control over the persistence logic
- Fine-grained control over transactions and sessions
- Low-level SQL optimizations
- Support for multiple databases with varying access patterns
When to Use Repository
- We are building a modern Spring Boot application
- We want to reduce boilerplate and increase development speed
- We are using Spring Data JPA, MongoDB, Neo4j, or other Spring Data modules
- Auto-generated CRUD operations
- Pagination, sorting, and filtering with minimal code
- Derived query methods like findByEmailAndStatus
Can You Use DAO and Repository Together?
Yes. In complex applications, you can:
- Use Spring Data Repositories for standard CRUD and query methods.
- Create custom DAO layers for complex, performance-critical operations.
Hybrid Example
public interface UserRepository extends JpaRepository<User, Long> {
// Auto-generated queries
List<User> findByStatus(String status);
}
@Repository
public class CustomUserDao {
@PersistenceContext
private EntityManager em;
public List<User> findUsersWithCustomLogic() {
// Custom JPQL or native SQL
return em.createQuery("SELECT u FROM User u WHERE u.active = true", User.class).getResultList();
}
}
Practical Comparison Table
Scenario | Use DAO | Use Repository |
Basic CRUD Operations | Manual implementation | Built-in |
Custom SQL with full control | Yes | Limited |
Legacy application | Preferred | Less common |
Modern Spring Boot application | Too verbose | Recommended |
Integration with Spring Data JPA | Requires manual integration | Seamless |
Auto-generated query methods | Not available | Available |
Conclusion
In Spring Boot, both DAO and Repository help manage data persistence, but they cater to different styles and application needs:
- Use DAO when you need custom implementations, fine-grained control, or are working on legacy systems.
- Use Repository for quick development, auto-generated queries, and Spring Data integrations.
- For most modern applications, Spring Data JPA repositories are the go-to solution due to their simplicity and power.
- However, DAO still has its place when maximum control is necessary.
By understanding both, we can choose the right abstraction based on your project’s complexity and future scalability.