users@glassfish.java.net

Bean Validation - using constraint sequencing and composition together

From: <glassfish_at_javadesktop.org>
Date: Wed, 21 Apr 2010 01:06:06 PDT

I am currently trying to use Bean Validation (JSR 303) in a Java EE 6 application deployed on Glassfish. This post has more to do with Bean Validation than Glassfish itself, so I appologise if this is not the correct forum for this post.

In this application I currently have an Entity class Person with a number of validations and the default validation sequence is changed with the @GroupSequence annotation. This code works fine as is, but I would like to merge the @Size, @Pattern and @IDNumberChecksum validations into a single @IDNumber validation without losing the ordering. Here is my working Person class.

Person.java
-----------------
@Entity
@GroupSequence({Person.class, IDNumberSizeGroup.class, IDNumberPatternGroup.class, IDNumberChecksumGroup.class})
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    private String firstName;

    @NotNull
    private String surname;

    @NotNull
    @Size(min = 13, max = 13, groups = {IDNumberSizeGroup.class})
    @Pattern(regexp = "^[\\d]{13}", groups = {IDNumberPatternGroup.class})
    @IDNumberChecksum(groups = {IDNumberChecksumGroup.class})
    //_at_IDNumber(groups = {IDNumberOrderedGroup.class})
    private String idNumber;

    // getters and setters
}

What I tried to do is create a new custom constraint called IDNumber annotated with the @Size, @Pattern and @IDNumberChecksum validations as above.

I also created an IDNumberOrderedGroup interface annotated with @GroupSequence and tried various combinations of sequences in both the Person class and IDNumberOrderedGroup interface, but cannot manage to get the constraint sequencing and constraint composition working together.

Any help will be appreciated and if I find the solution I will make sure to post it here.
[Message sent by forum member 'gadnex']

http://forums.java.net/jive/thread.jspa?messageID=398157