ConstraintViolationException:具有临时confirmPassword字段的用户实体 [英] ConstraintViolationException: User Entity with transient confirmPassword field

查看:219
本文介绍了ConstraintViolationException:具有临时confirmPassword字段的用户实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法提交春季表格.我遇到了这种异常,不知道为什么:

javax.validation.ConstraintViolationException: validation failed for classes [com.example.my.entities.User] during persist time for groups [javax.validation.groups.Default, ]

这是我的用户实体:

@Entity
@Table( name = "tbl_user" )
public class User implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue
  @Column( name = "id", nullable = false, columnDefinition = "serial" )
  private int id;

  @NotEmpty
  @Length( max = 255 )
  @Column( name = "username", unique = true, nullable = false, length = 255 )
  private String username;

  @NotEmpty
  @Column( name = "password", nullable = false, length = 32 )
  private String password;

  @NotEmpty
  @Transient
  private String confirmPassword;

  @NotEmpty
  @Email
  @Length( max = 255 )
  @Column( name = "email", unique = true, nullable = false, length = 255 )
  private String email;

  @Length( max = 255 )
  @Column( name = "role", length = 255 )
  private String role;

  @OneToMany( fetch = FetchType.LAZY, mappedBy = "id.user", cascade = { CascadeType.REMOVE }, orphanRemoval = true )
  private List<LicenseUserAlert> alerts;

  public int getId()
  {
    return id;
  }

  public void setId( int id )
  {
    this.id = id;
  }

  public String getUsername()
  {
    return username;
  }

  public void setUsername( String username )
  {
    this.username = username;
  }

  public String getPassword()
  {
    return password;
  }

  public void setPassword( String password )
  {
    this.password = password;
  }

  public String getConfirmPassword()
  {
    return confirmPassword;
  }

  public void setConfirmPassword( String confirmPassword )
  {
    this.confirmPassword = confirmPassword;
  }

  public String getEmail()
  {
    return email;
  }

  public void setEmail( String email )
  {
    this.email = email;
  }

  public String getRole()
  {
    return role;
  }

  public void setRole( String role )
  {
    this.role = role;
  }

  public List<LicenseUserAlert> getAlerts()
  {
    return alerts;
  }

  public void setAlerts( List<LicenseUserAlert> alerts )
  {
    this.alerts = alerts;
  }
}

我正确填写了表格,因此不应有任何约束违规... ConfirmPassword可能有问题吗?

解决方案

您应该考虑将confirmPassword@NotNull批注放入非默认验证组中.

即使我相信通过OR-Mapper验证@Transient字段也是一个错误.但是,JPA或Hibernate可能还有其他意见. -如果您可以验证问题是否属于该临时字段,并且同意我的观点,则可以在休眠错误跟踪器上打钩.

已添加 出于某种奇怪的原因,即使使用@Transient标记了字段,休眠也确实可以验证字段. 参见此论坛 https://forum.hibernate.org /viewtopic.php?f=9&t=1009600&start=0

另一个想法是不使用实体包含仅用于GUI的字段.因此,您应该将表单数据容器(命令对象)与实体对象分开.

@请参见在保存休眠实体时忽略临时模式

I'm not able to submit my spring form. I'm getting this exception and don't know why:

javax.validation.ConstraintViolationException: validation failed for classes [com.example.my.entities.User] during persist time for groups [javax.validation.groups.Default, ]

And here's my User entity:

@Entity
@Table( name = "tbl_user" )
public class User implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue
  @Column( name = "id", nullable = false, columnDefinition = "serial" )
  private int id;

  @NotEmpty
  @Length( max = 255 )
  @Column( name = "username", unique = true, nullable = false, length = 255 )
  private String username;

  @NotEmpty
  @Column( name = "password", nullable = false, length = 32 )
  private String password;

  @NotEmpty
  @Transient
  private String confirmPassword;

  @NotEmpty
  @Email
  @Length( max = 255 )
  @Column( name = "email", unique = true, nullable = false, length = 255 )
  private String email;

  @Length( max = 255 )
  @Column( name = "role", length = 255 )
  private String role;

  @OneToMany( fetch = FetchType.LAZY, mappedBy = "id.user", cascade = { CascadeType.REMOVE }, orphanRemoval = true )
  private List<LicenseUserAlert> alerts;

  public int getId()
  {
    return id;
  }

  public void setId( int id )
  {
    this.id = id;
  }

  public String getUsername()
  {
    return username;
  }

  public void setUsername( String username )
  {
    this.username = username;
  }

  public String getPassword()
  {
    return password;
  }

  public void setPassword( String password )
  {
    this.password = password;
  }

  public String getConfirmPassword()
  {
    return confirmPassword;
  }

  public void setConfirmPassword( String confirmPassword )
  {
    this.confirmPassword = confirmPassword;
  }

  public String getEmail()
  {
    return email;
  }

  public void setEmail( String email )
  {
    this.email = email;
  }

  public String getRole()
  {
    return role;
  }

  public void setRole( String role )
  {
    this.role = role;
  }

  public List<LicenseUserAlert> getAlerts()
  {
    return alerts;
  }

  public void setAlerts( List<LicenseUserAlert> alerts )
  {
    this.alerts = alerts;
  }
}

I filled in the form correctly, so there shouldn't be any constraint violation... Anything wrong with confirmPassword maybe?

解决方案

You should think of putting the @NotNull annotation of confirmPassword in an not default validation group.

Even if I belive that validation of an @Transient field by an OR-Mapper is an bug. But may JPA or Hibernate have an other opinion. -- If you can verify that the problem belongs to that transient field, and you agree to my opinion, then you may post a tickt at hibernates bug tracker.

Added For some strange reason hibernate is realy validating fields even if they are marked with @Transient. See this forum https://forum.hibernate.org/viewtopic.php?f=9&t=1009600&start=0

An other idea would be not to use a entity to contain a field that is only used for GUI. So may you should seperate the form data container (Command Object) from entity objects.

@See Ignore transient pattern on save hibernate entity

这篇关于ConstraintViolationException:具有临时confirmPassword字段的用户实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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