1

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?

dtrunk
  • 4,685
  • 17
  • 65
  • 109
  • The exception compains that the concrete data does not match the requirements. (Hibernate will check that constains too (you can configure that)) What are the concreate data of the entity you want to store? – Ralph Feb 13 '12 at 21:46
  • I am a bit worried about the seperator sign after the default group `[javax.validation.groups.Default, ]`. May it is only a not so well done formating issue, or hibernate really try to validate a second group without name. – Ralph Feb 13 '12 at 21:48
  • BTW: for password confirm have a look at http://stackoverflow.com/questions/1972933/cross-field-validation-with-hibernate-validator-jsr-303 – Ralph Feb 13 '12 at 21:51

1 Answers1

2

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

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • So I guess it isn't possible to put in another group without adding some java code? – dtrunk Feb 13 '12 at 22:11
  • i don't want to add an empty interface if it isn't necessary. but `@NotEmpty( groups = NoDefault.class )` + `public interface NoDefault{}` works... – dtrunk Feb 13 '12 at 22:17
  • Do not think of it like an empty interface, think of it like a marker ;-). See http://stackoverflow.com/questions/9257673/conditional-validations-in-spring/9258050#9258050 – Ralph Feb 13 '12 at 22:19
  • 1
    Wow, yeah I can do this. I just used the entity itself: `@NotEmpty( groups = User.class )` – dtrunk Feb 13 '12 at 22:23