Ade*_*ari 40 java orm dao hibernate jpa
我发现JPA,或类似的,不鼓励DAO模式.我不知道,但我觉得这样,尤其是服务器管理的JTA经理.
在使用DAO模式进行了充分的实践之后,我开始围绕该模式设计基于JPA的应用程序.但它不适合,IMO.我倾向于失去JPA和所有的功能.
好吧,假设您使用悲观锁定触发查询,并从DAO方法返回了一个entites列表.返回后,事务结束并且锁定消失(服务器管理的JTA管理器的情况).所以,没有意义,松散地说.但是有一些有效的案例.
另一个例子更为微不足道.假设您触发查询以获取某个实体,该实体具有延迟加载与其他实体的一对多关联.返回DAO方法后,事务结束.延迟加载将不再起作用,你只是得到null
或什么.为了应对这种情况,我们热切地手动加载它.我们做的事情就像a.getBList().size()
.
因此,IMO最好不要专门制作DAO,并且在您的业务bean中执行此操作,这样您就可以利用这些有用的功能.或者可以说ORM API可以被认为是DAO /数据层本身.所以,我们不需要另一个.
你们有什么想法呢?
注意:我没有说,DAO模式已经过时了.实际上,这取决于具体情况.
Pas*_*ent 47
对于简单的应用程序,我没有看到EntityManager
直接使用EJB和跳过DAO模式的任何问题(我厌倦了编写太多的代码).我的感觉确实是JPA和Java EE API所鼓励的.但对于更复杂的应用程序(从存储过程,平面文件中进行数据访问......),它仍然是合理的.所以你是对的,这取决于:)
在JPA中杀了DAO,你会发现其他一些开明的观点吗?在InfoQ上,你不会对内容和结论感到惊讶,这些内容和结论可以概括为:你不再需要DAO模式用于标准数据访问,但是你可能需要它用于一些更复杂的情况,但我们活着更好的没有它.
Boz*_*zho 31
If you don't define the DAO itself to be transactional, you won't have those problems.
The service layer is supposed to be transactional, because a transaction is supposed to span multiple operations. Putting each insert/update in a transaction is not the best scenario.
With spring you achieve that very easily. Without it you perhaps include the transaction logic in your DAO again - i.e. dao.beginTransaction()
and dao.commitTransaction()
and use that from the service layer instead.
As I understand, you suggest that using the EntityManager
directly in the service classes is probably better than having a wrapper DAO
class. I don't agree for one reason. Working the DAO class (interface at best) in your service classes, you don't have dependency on the JPA API at all. You don't have to construct Query
objects or the likes. This might not turn out to be a great advantage, but you'd agree it is a best practice. And you can later switch to plain JDBC, plain-text, XML, or whatever, by only changing the DAO.
This, although used widely as an example of why you should abstract something in another layer, is most often simply an overdesign. But sometimes the fact that all your database access operations are going through one place means you can add logging, access-level checks, etc. (Yes, sometimes the DAO is not a particularly suitable way to do this).
So ultimately, to return to your point - it depends.
归档时间: |
|
查看次数: |
14442 次 |
最近记录: |