Java 在EBean和表单中保存枚举列表

Java 在EBean和表单中保存枚举列表,java,playframework-2.0,ebean,Java,Playframework 2.0,Ebean,我有一个包含枚举项的字段“角色”。我正在尝试使用表单编辑此列表,但得到: [InvalidPropertyException: Invalid property 'roles[0]' of bean class [models.Device]: Property referenced in indexed property path 'roles[0]' is neither an array nor a List nor a Map; returned value was [SERVER]]

我有一个包含枚举项的字段“角色”。我正在尝试使用表单编辑此列表,但得到:

[InvalidPropertyException: Invalid property 'roles[0]' of bean class [models.Device]: Property referenced in indexed property path 'roles[0]' is neither an array nor a List nor a Map; returned value was [SERVER]]
这是我的实体:

@Entity
public class Device extends Model {

    @Id
    @Constraints.Min(1)
    public Long id;

    @Constraints.Required
    public String name;

    @Constraints.Required
    public String ipAddress;

    @Constraints.Required
    @ElementCollection(fetch = FetchType.EAGER)
    public Set<DeviceRole> roles = new HashSet<DeviceRole>(Arrays.asList(DeviceRole.OTHER));

    @Version
    public Timestamp lastUpdate;

    public static Finder<Long,Device> find = new Finder<Long,Device>(
      Long.class, Device.class
    );

    public List<ValidationError> validate() {
        /*
        List<ValidationError> errors = new ArrayList<ValidationError>();
        if (User.byEmail(email) != null) {
            errors.add(new ValidationError("email", "This e-mail is already registered."));
        }
        return errors.isEmpty() ? null : errors;
        */
        return null;
    }
}
@实体
公共类设备扩展模型{
@身份证
@限制条件。最小值(1)
公共长id;
@约束条件。必需
公共字符串名称;
@约束条件。必需
公共字符串IP地址;
@约束条件。必需
@ElementCollection(fetch=FetchType.EAGER)
公共集角色=新哈希集(Arrays.asList(DeviceRole.OTHER));
@版本
公共时间戳lastUpdate;
公共静态查找器find=新查找器(
Long.class、Device.class
);
公共列表验证(){
/*
列表错误=新建ArrayList();
if(User.byEmail(email)!=null){
添加(新的ValidationError(“电子邮件”,“此电子邮件已注册”);
}
返回错误。isEmpty()?null:错误;
*/
返回null;
}
}
控制器中的编辑和更新功能:

public static Result edit(Long id) {
    Device device = Device.find.byId(id);
    Form<Device> myForm = Form.form(Device.class);
    myForm = myForm.fill(device);

    return ok(views.html.Devices.edit.render(myForm, listOfRoles()));
}

public static Result update() {
    Form<Device> deviceForm = Form.form(Device.class).bindFromRequest();

    if (deviceForm.hasErrors()) {
        return badRequest(views.html.Devices.edit.render(deviceForm, listOfRoles()));
    }

    // Form is OK, has no errors, we can proceed
    Device device = deviceForm.get();
    device.update(device.id);
    return redirect(routes.Devices.index());
}

private static List<String> listOfRoles() {
    List<String> list = new ArrayList<String>();
    for(DeviceRole role : DeviceRole.values()) {
        list.add(role.toString());
    }
    return list;
}
公共静态结果编辑(长id){
Device Device=Device.find.byId(id);
Form myForm=Form.Form(Device.class);
myForm=myForm.fill(设备);
返回ok(views.html.Devices.edit.render(myForm,listOfRoles());
}
公共静态结果更新(){
Form deviceForm=Form.Form(Device.class).bindFromRequest();
if(deviceForm.hasErrors()){
返回badRequest(views.html.Devices.edit.render(deviceForm,listOfRoles());
}
//表格正常,没有错误,我们可以继续
Device Device=deviceForm.get();
设备更新(设备id);
返回重定向(routes.Devices.index());
}
私有静态列表listOfRoles(){
列表=新的ArrayList();
for(DeviceRole角色:DeviceRole.values()){
添加(role.toString());
}
退货清单;
}
以及模板:

@main("Edit a device") {

  @helper.form(action = routes.Devices.update()) {
      @helper.inputText(myForm("name"))
      @helper.inputText(myForm("ipAddress"))

      @helper.select(myForm("roles"), options(deviceRoles), 'multiple->"multiple")

      <input type="hidden" name="id" value="@myForm("id").value">
      <input type="submit" value="Submit">
  }

  <a href="@routes.Devices.index()">Cancel</a>
}
@main(“编辑设备”){
@helper.form(action=routes.Devices.update()){
@helper.inputText(myForm(“名称”))
@helper.inputText(myForm(“ipAddress”))
@选择(myForm(“角色”)、选项(deviceRoles)、“多个->“多个”)
}
}

实际上,这样做可能需要将它们保存为字符串字段中以逗号分隔的值列表,为了更清楚,我建议将
DeviceRole
更改为Ebean模型,并将其作为常见的多对多关系处理

它为IMHO提供了更大的灵活性,也允许您在未来创建更具活力的角色

只需分叉您的示例并对其进行修改,以显示我将如何自己完成(自述文件中的一些详细信息):


注意:如果到目前为止没有任何更改,请查看Ebean的记录单,只是不支持保存枚举列表(我不知道当前状态)

对于枚举,您可以使用
@EnumValue
,这是一个特定于Ebean的注释,用于将枚举映射到数据库值

public static enum RoleName {
  @EnumValue("SERVER") SERVER,
  @EnumValue("TEMPERATURE") TEMPERATURE,
  @EnumValue("HUMIDITY") HUMIDITY,
  @EnumValue("SWITCH") SWITCH,
  @EnumValue("OTHER") OTHER
}
参考: