ConstraintViolationException: User Entity with tra

2019-06-03 00:30发布

问题:

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?

回答1:

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