问题描述
如果我使用像JPA2的ORM - 在那里我有我的实体映射到我的数据库,我应该还是使用DAO?这似乎是一个很多的开销。
例如,我需要保持三个额外的软件包:
-
之一,指定我的域对象(pretty多少映射我的实体对象):
公共类Employee {
私有String的firstName;
私人字符串的lastName;
...
// getter和setter方法
} -
一个包含指定吾道方法接口
公共接口EmployeeDAO {
公共无效addEmployee(员工员工);
公务员getEmployeeById(长ID);
...
} -
一个包含实现我的DAO的。
会话bean公共EmployeeJpaDAO实现EmployeeDAO {
这里的接口方法实现
....
那个改变我Employee实体进入我的员工的域对象的私有方法
}
现在这是一个很大微胖加我每次需要执行一个新的CRUD操作时间。
不过,我从有一个DAO看到它的好处是:
-
您可以在内存中执行的DAO有一个单元测试你的服务层。这意味着你不需要访问数据库来测试业务逻辑,你可以放心,你的对象总是包含的属性相同的值
-
它的业务逻辑从数据库访问逻辑分离
这不涉及实施DAO是只用在服务层实体对象和EntityManager的选项:
@Stateless
公共类EmployeeEjb {
@PersistenceContext(=的unitName雇员)
私人EntityManager的经理; 公务员getEmployeeById(长ID){
返回manager.createNamedQuery(Employee.GetEmployeeById).getResultList();
}
...
}
有没有中间地带吗?有没有人碰到一个架构或实施符合一定的DAO层,我上面提到的,但不涉及所有参与来实现DAO层的开销(业务逻辑最重要的是可测性单位)的优点的架构?
感谢您的任何建议和/或建议!我真的很好奇,想看看有些人想出问候了这一点。
It is. And clearly, Java EE doesn't encourage using the DAO pattern when using JPA (JPA already provides a standardized implementation of the Domain Store pattern and there isn't much value at shielding it behind a DAO). I find the DAO to be anti-DRY in such situation.
So for simple cases (actually, most cases), I happily skip the DAO and I have no problem with that. For more complex cases (for example when using stored procedures, flat files), I'd use it. In other words, it depends, as summarized in Has JPA Killed the DAO?. See also the related questions below:
Related questions
- I found JPA, or alike, don't encourage DAO pattern
- Simple but good pattern for EJB
- What is a good strategy for seprating layers for an appication that can be used online and offline?
- Using JSF, JPA and DAO. Without Spring?
- What's an appropriate DAO structure with jpa2/eclipselink?
Noooo, you certainly don't want to implement a DAO as a Session Bean:
- You don't want to create as much (pooled) Session Bean as tables (big waste of resources)
- You don't want to chain Session Beans everywhere, don't reproduce errors from the past, this is a known bad practice that doesn't scale well.
So if you really want to go the DAO way and want the EM to be injected, either implement your DAOs as Spring beans (in Java EE 5) or CDI managed bean (in Java EE 6).
If you really want to do unit testing, mock the DAO/EntityManager, there is no difference. And if you want to do integration testing, you can configure JPA to use an in memory database. So at the end, I just don't buy this argument.
Honestly, I don't see a big difference between relying on a DAO vs an entity manager, I don't see how a DAO separate things "better". Again, I don't buy this argument.
And to my experience, changing the underlying persistence solution is a very exceptional event and I'm not going to introduce DAOs for something that is very likely not going to happen (YAGNI, KISS).
I don't see much middle ground and, as strongly hinted, I don't use DAOs if I don't feel the need. And as I said, mock the EntityManager
if you want to truly unit test the business logic. It works for me and I'm happy to write less code.
More resources
- JPA/EJB3 killed the DAO
- DAOs Aren't Dead - But They Either Collapsed Or Disappeared
- ...And Finally, You Should Introduce A DAO If:
这篇关于Java EE的建筑 - 是DAO的使用JPA一样的2时ORM仍建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!