简体   繁体   English

实体没有表单对象所需的字段

[英]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.

相关问题 如何编写Java类以从没有固定字段的Json获取对象? - How to write Java class to fetch the object from Json which doesn't have fixed fields? 创建不映射到实体的表单 - Creating a form that doesn't map to an entity 杰克逊反序列化JSON,跳过没有必填字段的对象? - Jackson Deserializing JSON, skip objects that don't have required fields? JPA CriteriaQuery:未映射对象的字段 - JPA CriteriaQuery: fields of the object doesn't mapped Android 应用程序没有互联网连接 - 无需权限 - Android App doesn't have internet connection - No Permissions required 如何使用Spring Data MongoDB从Entity到VO对象获取必需的字段数据 - How to get the required fields data from an Entity to VO object using Spring Data MongoDB 可以使用不使用数据库的实体类吗? - Is it okay to have entity class that doesn't use database? 具有复合主键的实体类没有getter和setter - Entity class with a composite primary key doesn't have getter and setter 为什么Java编译器无法识别字段已初始化? - Why doesn't the java compiler recognise fields have been initialized? 按没有关系的字段加入 criteriabuilder 中的表 - Joining table in criteriabuilder by fields witch doesn`t have relations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM