1

I am working with hibernate framework and I want to apply hibernate constraint on my @Entity class. I want to execute/run constraint on it's order/sequence as define inside @Entity class.

@Entity
@Table(name = "User")
public class User{
    @NotNull
    @NotBlank
    @Pattern(regexp = "[a-zA-Z]+")
    @Length(max = 10)
    private String firstName;
}

I want to execute constrain as below order:

    1) @NotNull
    2) @NotBlank
    3) @Pattern(regexp = "[a-zA-Z]+")
    4) @Length(max = 10)

Any one please help me how can I achieve it. I also want to apply constrain order/sequence on more than one field.

Piyush Chaudhari
  • 962
  • 3
  • 19
  • 41

1 Answers1

0

There are Group and GroupSequence concepts in Hibernate validations. Check them. If I apply those concepts your code will look like below.

@Entity
@Table(name = "User")
@GroupSequence({One.class, Two.class, Three.class, Four.class})
public class User{
    @NotNull(groups={One.class})
    @NotBlank(groups={One.class})
    @Pattern(regexp = "[a-zA-Z]+" ,groups={One.class})
    @Length(max = 10,groups={One.class})
    private String firstName;
}
Anil
  • 595
  • 1
  • 5
  • 15