-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Bean Validation - using constraint sequence and composition
PostPosted: Wed Apr 21, 2010 6:11 am 
Newbie

Joined: Wed Apr 21, 2010 5:58 am
Posts: 2
I am currently trying to use Bean Validation (JSR 303) in a Java EE 6 application deployed on Glassfish.

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
-----------------
Code:
@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})
//@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.


Top
 Profile  
 
 Post subject: Re: Bean Validation - using constraint sequence and composition
PostPosted: Fri Apr 23, 2010 7:50 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
I don't think this is currently possible. The JSR 303 spec says regarding groups and constraint composition:
Quote:
Groups from the main constraint annotation are inherited by
the composing annotations. Any groups definition on a composing annotation is ignored.


--Hardy


Top
 Profile  
 
 Post subject: Re: Bean Validation - using constraint sequence and composition
PostPosted: Fri Apr 23, 2010 8:51 am 
Newbie

Joined: Wed Apr 21, 2010 5:58 am
Posts: 2
Thanks for the info. I didn't see this statement in the JSR 303 spec. After further experimentation I found that the groups definition on the composing annotation is indeed ignored.

What I did in the end is to ensure that my validations are mutually exclusive, so that no matter in which order they are executed, only one violation will occur.

I originally wanted the validations to occur in the order @Size, @Pattern and then @IDNumberChecksum. I changed the @Pattern regex to ^[\\d]* so that it does not assume a idNumber length of 13. In the @IDNumberChecksum I also had to test for the length and the same regex pattern and retturn true.

The only problem is that I needed to code the same validations twice, once using annotations and once in Java custom validator code. Also if a person enters and idNumber for example 123A5, I would not be sure whether the @Size or @Pattern violations would occur first.

What I would propose for a next version of the JSR 303 spec is to allow groups within a constraint composition, which are ignored as they are now, unless the constraint composition is anotated with the @GroupSequence annotation as was done with my original Person class.

Maybe this idea would not be implementable, but I think it deserves some consideration at least.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.