我无法提交我的弹簧表格。我得到了这个例外,不知道为什么:
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有关的错误吗?
答案 0 :(得分:2)
您应该考虑将@NotNull
注释confirmPassword
放在非默认验证组中。
即使我相信OR-Mapper验证@Transient
字段是一个错误。但JPA或Hibernate可能有其他意见。 - 如果您可以验证问题是否属于该瞬态字段,并且您同意我的意见,那么您可以在hibernates bug跟踪器上发布一个tickt。
<强>加强> 出于一些奇怪的原因,即使用@Transient标记了hibernate,它仍然可以验证字段。 查看此论坛https://forum.hibernate.org/viewtopic.php?f=9&t=1009600&start=0
另一个想法是不使用实体来包含仅用于GUI的字段。所以你可以从实体对象中分离表单数据容器(命令对象)。