我发现JPA或类似的,不鼓励DAO模式 [英] I found JPA, or alike, don't encourage DAO pattern

查看:163
本文介绍了我发现JPA或类似的,不鼓励DAO模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现JPA或类似的,不鼓励DAO模式。我不知道,但我觉得这样,尤其是在服务器管理的JTA管理人员身上。



经过充分的动手使用DAO模式后,我开始设计基于JPA的应用程序那种模式。但它不适合IMO。我倾向于失去JPA和所有功能。

好吧,假设您使用悲观锁定启动查询,并返回DAO方法中的实体列表。返回后,事务结束并锁定消失(服务器管理的JTA管理器的情况)。所以,没有意义,松散地说。尽管如此,还是有效的。



另一个例子更简单。假设你激发了一个查询来获得一个实体,它与其他实体有一个延迟加载的一对多关联。在返回DAO方法后,事务结束。延迟加载不再有效,您只需获取 null 或其他内容即可。为了应对这个问题,我们手动加载它。我们做类似于 a.getBList()。size()



因此,IMO最好不要使一个DAO,并在你的业务bean中执行,这样你就可以利用这些有用的功能。或者可以说ORM API本身可以被认为是一个DAO /数据层。所以,我们不需要做另一个。



你们在想什么?

注意:我不会说,无论如何,DAO模式已经过时。事实上,这取决于个案。 解决方案

对于简单的应用程序,使用 EntityManager 直接从EJB跳过DAO模式(我厌倦了编写太多的代码)。我的感觉确实是这是JPA和Java EE API所鼓励的。但是对于更复杂的应用程序(从存储过程的数据访问,平面文件...),它仍然是合理的。所以你是对的,它取决于:)



你会在 InfoQ上有JPA杀死了DAO?,但您不会对内容和结论感到惊讶,可以概括为:不再需要标准数据访问的DAO模式,但您可能需要它来适应更复杂的情况,但如果没有它,我们的生活会更好。


I found JPA, or alike, don't encourage DAO pattern. I don't know, but I feel like that, especially with server managed JTA managers.

After adequate hands-on using DAO pattern, I started designing JPA based application around that pattern. But it doesn't fit in, IMO. I tend to lose quite a features of JPA and all.

Well, suppose you fire a query with pessimistic locking and it returned a list of entites from a DAO method. Upon returning, transaction ends and lock is gone (a case with server managed JTA manager). So, no point, loosely speaking. There are valid cases, though.

Another example is much more trivial. Suppose you fire a query to get some entity, that has a lazy loading one-to-many association with some other entity. Upon returning the DAO method, transaction ends. Lazy loading wouldn't work anymore, you simply get null or something. To cope with that we load it eagerly manually. we do something like a.getBList().size().

Thus, IMO its better to not make a DAO exclusively, and do it in your business bean, this way you will be able to take advantage of those useful features. Or ORM API can be considered a DAO/Data-layer itself, arguably. So, we don't need to make another.

What you folks think about it?

Note: I don't say, by any means, that the DAO pattern is obsolete. Indeed it depends case to case.

解决方案

For simple applications, I don't see any problem in using the EntityManager directly from EJBs and skipping the DAO pattern (I'm tired of writing too much code). And my feeling is indeed that this is what JPA and the Java EE API encourage. But it may still be justified for more complex applications (for data access from stored procedure, flat files...). So you are right, it depends :)

You'll find some other enlightened point of views in Has JPA Killed the DAO? on InfoQ but you won't be surprised by the content and the conclusion that can be summarized as: you don't really need the DAO pattern anymore for standard data access, you may however need it for some more complex situations, but we live better without it.

这篇关于我发现JPA或类似的,不鼓励DAO模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆