I need password validation (for example a password must contain at least 4 chars, max 8 and min 1 numeric digit). I have model (of course with getters and setters):
@Entity
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 5534441879591858724L;
@Id
@GeneratedValue
private long id;
@NotBlank
@Email
private String email;
@Pattern(regexp = "^(?=.*\\d).{4,8}$", flags = Flag.UNICODE_CASE)
private String password;
@NotBlank
@Size(min=2, max=30)
private String name;
I'm catching ConstraintViolationException during saving user info to database and use informations from this exception to inform the user during registration what fields must be corrected because of invalid length etc.
Everything is ok with validation, but not with password. I checked regex expression out of this model class and it works ok, but when i put this regex into annotation parameter ( @Pattern(regexp = "^(?=.*\\d).{4,8}$", flags = Flag.UNICODE_CASE)
) it doesn't work and I have an error:
HHH000346: Error during managed flush [Validation failed for classes [pl.rpf.kingdom.models.User] during persist time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='must match "^(?=.*\d).{4,8}$"', propertyPath=password, rootBeanClass=class pl.rpf.kingdom.models.User, messageTemplate='{javax.validation.constraints.Pattern.message}'} ]]
Please help me with understanding this error and maybe you have some idea how to solve this problem.