[hibernate-users] GlassFish, JSR-303, Custom Annotation does not fire

Roger Varley roger.varley at googlemail.com
Sun Nov 14 15:12:48 EST 2010


I'm just starting JEE6 with Glassfish and JSF, and I'm trying to create
my first JSR-303 validation. The problem I've got is that despite the
fact that everything compiles, the validator class is never called. I've
listed the code below. (I know that the validator doesn't make logical
sense at the moment, there's more to go in it, I'm just trying to get it
to work at the moment.) Can anyone see what I'm doing wrong.


The annotation;

 package com.vasilikon.pembridge.validators;  
 import com.vasilikon.pembridge.entities.TelephoneType;  
 import static java.lang.annotation.ElementType.*;  
 import static java.lang.annotation.RetentionPolicy.*;  
 import java.lang.annotation.Documented;  
 import java.lang.annotation.Retention;  
 import java.lang.annotation.Target;  
 import javax.validation.Constraint;  
 import javax.validation.Payload;  
@Constraint(validatedBy = PhoneNumberValidator.class)  
public @interface PhoneNumberCheck  
     String message() default "Invalid Telephone number.";  
     Class<?>[] groups() default {};  
     Class<? extends Payload>[] payload() default {};  
     TelephoneType value();  

The validator;

  package com.vasilikon.pembridge.validators;  
 import com.vasilikon.pembridge.entities.TelephoneType;  
 import javax.validation.ConstraintValidator;  
 import javax.validation.ConstraintValidatorContext;  

 public class PhoneNumberValidator implements
ConstraintValidator<PhoneNumberCheck, Object>  
      private TelephoneType type;  
      public void initialize(PhoneNumberCheck constraintAnnotation) {  
        this.type = constraintAnnotation.value();  
      public boolean isValid(Object value, ConstraintValidatorContext
context) {  
           try {  
               System.out.println("isValid called");  // Never gets
               long telephoneNumber = ((Long) value).longValue();  
               if (type == TelephoneType.MOBILE) {  
               } else {  
           } catch (Exception e) {  
           /* Do Nothing */  
           return true;  
      private boolean isValidMobileNumber(ConstraintValidatorContext
context, long telephoneNumber, TelephoneType type) {  
            * Mobile number is exactly 8 digits long 
            * and starts 99nnnnnn 
           boolean valid =  ((telephoneNumber > 99000000)  
                               && (telephoneNumber < 999999999));  
           if (!valid) {  
               context.buildConstraintViolationWithTemplate("A mobile
number must be 8 digits starting with 99")  
          return valid;  
       private boolean isValidLandLineNumber(ConstraintValidatorContext
context, long telephoneNumber, TelephoneType type) {  
          throw new UnsupportedOperationException("Not yet

and my backing bean (trimmed);

public class PhoneNumberPromptBean {  
     PembridgeServices services;  
     @NotNull(message="You must select a number type")  
     private TelephoneType type;  

     private long telephoneNumber;  
  <Getters and Setters>  

More information about the hibernate-users mailing list