简体   繁体   English

POJO或DTO方法

[英]POJO or DTO approach

I am developing a new web application with Struts2, Spring and Hibernate as its core building blocks. 我正在开发一个新的Web应用程序,Struts2,Spring和Hibernate作为其核心构建块。

We have created POJO classes with respect to hibernate mapping files.There will be some inputs from users which needs to be updated in to the underlying database 我们已经创建了关于hibernate映射文件的POJO类。来自用户的一些输入需要更新到底层数据库

eg registration or updation. 例如注册或更新。

We have few option like creating new POJO/DTO for action classes which will be filled by the Struts2 and than we can transfer them to the service layer where we can convert those DTO to the respected hibernate POJO else we can expose same POJO to struts2 so that the framework can fill them with the user input and we need not to do the work for conversion and creating extra set of classes. 我们几乎没有选择为动作类创建新的POJO / DTO,这些动作类将由Struts2填充,而不是我们可以将它们转移到服务层,在那里我们可以将这些DTO转换为受尊重的hibernate POJO,否则我们可以将相同的POJO暴露给struts2所以框架可以用用户输入填充它们,我们不需要进行转换工作和创建额外的类集。

Application will be not big in size and will have a medium size application tag. 应用程序的大小不会很大,而且应用程序标签中等。

My question is what is the best way to transfer this user input to underlying hibernate layer to perform data base specific work. 我的问题是,将此用户输入传输到底层hibernate层以执行特定于数据库的工作的最佳方法是什么。

Thanks in advance 提前致谢

I'd prefer the "DTO" approach in this case since you then can validate the input first and trigger updates only when wanted. 在这种情况下,我更喜欢“DTO”方法,因为您可以先验证输入并仅在需要时触发更新。

However, you could use detached entities as DTOs and reattach them when you want to create or update them. 但是,您可以将分离的实体用作DTO,并在要创建或更新它们时重新附加它们。 If you don't want the web part of your application to depend on Hibernate and/or JPA you might need to create another set of classes (unless you don't use a single annotation). 如果您不希望应用程序的Web部分依赖于Hibernate和/或JPA,则可能需要创建另一组类(除非您不使用单个注释)。

You'll get both answers on this. 你会得到这两个答案。

With Struts 2 I tend to use normal S2 action properties to gather form values/etc. 使用Struts 2,我倾向于使用普通的S2动作属性来收集表单值/等。 and use BeanUtils to copy them to the Hibernate objects. 并使用BeanUtils将它们复制到Hibernate对象。 The problem with exposing the Hibernate objects to the form, like with ModelDriven etc. is that you need to define whitelists/blacklists if you have columns that should not be set directly by the user. 使用Hibernate的对象与暴露的形式,如问题ModelDriven等,是你需要定义白名单/黑名单,如果你有一个应该由用户直接设置列。 (Or handle the problem in a different way.) (或者以不同的方式处理问题。)

That said, I'm not fundamentally opposed to the idea like a lot of people are, and they're arguably correct. 也就是说,我并没有像许多人那样从根本上反对这个想法,而且他们可以说是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM