[英]Entity doesn't have fields required for Form Object
I have Entity: 我有实体:
public class User {
private Long id;
private String name;
private String lastName;
private String email;
private String password;
// getters and setters
}
I would like to use it as a Form Backing Object in presentation layer, but it doesn't have fields that I need. 我想将其用作表示层中的表单支持对象,但是它没有我需要的字段。 In this case I need repeatedPassword field.
在这种情况下,我需要repeatPassword字段。
What is the best approach to this problem without modifying entity class? 在不修改实体类的情况下解决此问题的最佳方法是什么?
Should I extend this entity class and add needed fields? 我应该扩展此实体类并添加所需的字段吗?
Should I create new class which has a field private User user;
我应该创建一个具有
private User user;
字段的新类private User user;
with getter and setter? 与吸气剂和二传手?
Should I copy/paste this class and then add needed fields? 我应该复制/粘贴此类,然后添加所需的字段吗?
Three different solutions: 三种不同的解决方案:
The usual way of dealing with a situation where the input fields on a form don't match up with an entity is to use a Command object (this is what Sotirios was advocating in the comments). 处理表单上的输入字段与实体不匹配的情况的通常方法是使用Command对象(这是Sotirios在注释中主张的内容)。 Making a separate object for this that is not an entity is much less confusing than extending the entity to add something unrelated to persistence.
为此,创建一个不是实体的单独对象要比扩展实体添加与持久性无关的东西要容易得多。
It be argued in this case there's no need to pass the repeatedPassword
field back to the server, the validation could be done entirely on the client, and the input field doesn't need to be bound to a Java pojo at all. 在这种情况下,有人争辩说,不需要将
repeatedPassword
字段传递回服务器,可以完全在客户端上进行验证,并且完全不需要将输入字段绑定到Java pojo。
If you really want to pass the repeatedPassword
back to the server (so you can do all your validation on the server-- although the repeated password is more of a user convenience than real validation), then you could add the repeatedPassword
field to the User entity with a @Transient declaration. 如果你真的想通过
repeatedPassword
回服务器(所以你可以做的server--所有的验证虽然反复密码更多的是一种方便用户不是真正的验证),那么你可以在添加repeatedPassword
领域用户具有@Transient声明的实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.