DAO is still needed in Spring MVC with JPA / Hibernate

I know that this question has been asked countless times, but since most of the questions on this question are 6-7 years old, I was hoping to see if there are any changes in the original arguments for / against this topic, since newer versions of JPA have appeared. I know that this can be considered as based primarily on opinion, but I am looking for a list of pluses / minuses.

Some people have claimed that the entitymanager itself is a DAO and that the DAO level in your application will be redundant. Most people will agree that EntityManagervery much covers the basic CRUD operations ... but there is a point at which we should not use entitymanager.createQuery(...)inside the service level.

But now with the annotation, @NamedQuerieswe can simply add named queries to the model class and maintain a set of custom criteria queries for each entity model. For example, if we had a class User, we could have something like

@Entity
@NamedQueries({
  @NamedQuery(name="User.findByUsername", query="SELECT u FROM User u WHERE u.username_lowercase = LOWER(:username)")
})
public class User{

@Column
private String username;

}

By storing custom criteria queries within model classes, we can avoid re-writing the same queries over and over at the service level, which makes the DAO layer even more unnecessary.

The reasons I found to enable the DAO layer now:

  • You can more easily change the persistence mechanism in the future by having a central point that supports data access (i.e. a common DAO)
  • Simple unit time testing

I was wondering if anyone would be able to contribute to this list, since I'm not sure if I should use the DAO layer in my application.

+6
1

, , , , EntityManager DAO, .

, crud - EntityManager + @NamedQueries .

, - DTO ( ), DAO , , , - .

, CRUD, DAO, , .

+5

All Articles