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

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

问题描述

我发现 JPA 或类似的东西不鼓励 DAO 模式.我不知道,但我有这种感觉,尤其是服务器管理的 JTA 管理器.

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.

在使用 DAO 模式进行了充分的实践之后,我开始围绕该模式设计基于 JPA 的应用程序.但它不适合,IMO.我往往会失去 JPA 和所有的一些特性.

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.

好吧,假设您使用悲观锁定触发查询,并且它从 DAO 方法返回了一个实体列表.返回时,事务结束并且锁消失(服务器管理的 JTA 管理器的情况).所以,松散地说,没有意义.不过,也有一些有效的案例.

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.

另一个例子要简单得多.假设您触发查询以获取某个实体,该实体与某个其他实体具有延迟加载的一对多关联.返回 DAO 方法后,事务结束.延迟加载将不再起作用,您只会得到 null 或其他东西.为了解决这个问题,我们急切地手动加载它.我们做一些类似 a.getBList().size().

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().

因此,IMO 最好不要专门制作 DAO,而是在您的业务 bean 中制作,这样您就可以利用这些有用的功能.或者 ORM API 可以被认为是 DAO/数据层本身,可以说.所以,我们不需要制作另一个.

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.

大家怎么看?

注意:无论如何,我并不是说 DAO 模式已经过时.事实上,这取决于具体情况.

推荐答案

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

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 :)

您会在 是否 JPA 杀死了 DAO 中找到了一些其他开明的观点? 在 InfoQ 上,但您不会对可以总结为的内容和结论感到惊讶:您实际上不再需要 DAO 模式来进行标准数据访问,但是您可能需要它来处理一些更复杂的事情情况,但没有它我们生活得更好.

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天全站免登陆