如何在 JSF + Spring + Hibernate 中使用 DTO [英] How to use DTO in JSF + Spring + Hibernate

查看:22
本文介绍了如何在 JSF + Spring + Hibernate 中使用 DTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我是 DTO 主题的新手.我不明白将 DTO 与 JSF、Spring 和 Hibernate 一起使用是否正确.
让我解释一下,到目前为止,我在业务层和表示层都使用了直接从数据库创建的实体 bean.现在我决定尝试使用 DTO 方法,但我无法理解它们如何提供帮助.
例如,如果我有两个类 User 和 Message,并且一个用户关联了更多消息;如何从数据库中填充 DTO?还是我在业务层手动填充 DTO?有人可以发布一个关于如何使用 DTO 的示例吗?

Assuming that i'm new about the topic DTO. I can't understand if it is correct to use the DTO in tandem with JSF, Spring and Hibernate.
Let me explain, so far I have used the entity bean, created directly from the database, both in business layer and in the presentation layer. Now I decided to try using the DTO approach, but I can not understand how they can help.
For example if I have two classes User and Message, and a user has more messages associated; how can I populate the DTO from the database? Or do I manually populate the DTO at the business layer? can someone post an example on how to use DTO?

先谢谢你.问候,罗伯托

Thank you in advance. Regards, Roberto

推荐答案

DTO 代表数据传输对象.它应该是一个普通的 Javabean 类,没有任何 API/架构特定的限制,例如 JSF、JPA 或 Spring 注释.IE.它不应包含任何指向外部 API 的 import 或 FQN.唯一的目标是能够在大型模块化 Web 应用程序的不同架构之间传递数据.

DTO stands for Data Transfer Object. It's supposed to be a plain vanilla Javabean class without any API/architecture specific restrictions such as JSF, JPA or Spring annotations. I.e. it should not contain any import or FQN pointing to an external API. The sole goal is to be able to pass data between different architectures of a big modular webapplication.

例如,如果您不想使用 JPA/Hibernate 实体 bean 作为 JSF 托管 bean 和视图的模型属性,因为由于某些过于严格的业务或模块化原因,它们可能不会被传递到 EJB 类之外,那么您需要创建此类的副本并自己映射松散的属性.基本上:

For example, if you don't want to use a JPA/Hibernate entity bean as model property of a JSF managed bean and view because they may not be passed beyond the EJB class due to some overly restrictive business or modularity reasons, then you need to create a copy of this class and map the loose properties yourself. Basically:

UserEntity userEntity = em.find(UserEntity.class, id);
UserDTO userDTO = new UserDTO();
userDTO.setId(userEntity.getId());
userDTO.setName(userEntity.getName());
// ...
return userDTO;

大量的库可以通过以下方式使 bean 到 bean 映射变得容易:

There are plenty of libraries available which makes bean-to-bean mapping easy in following way:

SomeLibary.map(userEntity, userDTO);

但是,对于一般的 Web 应用程序,您不需要 DTO.您已经在使用 JPA 实体.您可以继续在 JSF bean/视图中使用它们.

However, for the average webapplication you don't need DTOs. You're already using JPA entities. You can just go ahead with using them in your JSF bean/view.

仅此问题就已经表明您实际上根本不需要 DTO.您不会被某些特定的业务限制阻止.然后你不应该搜索设计模式,以便你可以将它应用到你的项目中.您应该以过于复杂/不可维护/重复的代码的形式搜索真正的问题,以便您可以为其询问/找到合适的设计模式.通常,重构重复代码几乎会自动引入新的设计模式,而您并未真正意识到这一点.

This question alone already indicates that you actually don't need DTOs at all. You are not blocked by some specific business restrictions. You should not then search for design patterns so that you can apply it on your project. You should rather search for real problems in form of overcomplicated/unmaintainable/duplicated code so that you can ask/find a suitable design pattern for it. Usually, refactoring duplicate code almost automatically introduces new design patterns without you actually realizing it.

一个很好的例子是当 JPA 实体太大"时出于特定目的(即实体包含的属性比您实际需要的多得多).拥有大量那些部分使用的实体会浪费服务器内存.要解决此问题,您可以仅基于使用 构造函数表达式 JPQL.

A good example is when a JPA entity is "too large" for the particular purpose (i.e. the entity contains way much more properties than you actually need). Having a lot of those partially used entities is a waste of server memory. To solve this, you could create a DTO class/subclass based on only a few of its properties which you create and fill using constructor expression in JPQL.

这篇关于如何在 JSF + Spring + Hibernate 中使用 DTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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