Basically I want to use the javax.validation.constraints.Past annotation with with my own timestamp class.
So I wrote a ConstraintValidator:
Code:
public class PastValidator implements ConstraintValidator<Past, Timestamp>
{
/* (non-Javadoc)
* @see javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation)
*/
@Override
public void initialize(final Past past) { }
/* (non-Javadoc)
* @see javax.validation.ConstraintValidator#isValid(java.lang.Object, javax.validation.ConstraintValidatorContext)
*/
@Override
public boolean isValid(final Timestamp tsParam, final ConstraintValidatorContext context) {
return (tsParam==null || tsParam.getTime()<System.currentTimeMillis());
}
}
I'm trying to then validate a timestamp being passed to a method:
Code:
public void method(@Past Timestamp t) {
...
}
using AspectJ to inject the validation code. What I get is this:
Quote:
javax.validation.UnexpectedTypeException: No validator could be found for type: com.test.Timestamp
at org.hibernate.validator.engine.ConstraintTree.verifyResolveWasUnique(ConstraintTree.java:383)
at org.hibernate.validator.engine.ConstraintTree.findMatchingValidatorClass(ConstraintTree.java:364)
at org.hibernate.validator.engine.ConstraintTree.getInitializedValidator(ConstraintTree.java:313)
at org.hibernate.validator.engine.ConstraintTree.validateConstraints(ConstraintTree.java:144)
at org.hibernate.validator.engine.ConstraintTree.validateConstraints(ConstraintTree.java:117)
at org.hibernate.validator.metadata.MetaConstraint.validateConstraint(MetaConstraint.java:84)
at org.hibernate.validator.engine.ValidatorImpl.validateParameterForGroup(ValidatorImpl.java:1001)
at org.hibernate.validator.engine.ValidatorImpl.validateParametersForGroup(ValidatorImpl.java:934)
at org.hibernate.validator.engine.ValidatorImpl.validateParametersInContext(ValidatorImpl.java:864)
at org.hibernate.validator.engine.ValidatorImpl.validateAllParameters(ValidatorImpl.java:225)
Looking at the validators that hibernate uses internally, I can't see any difference to how they support Date and Calendar. Does anyone have any ideas on how to make this work? Do I have to define my own Annotation for Past for my custom class?
FYI, I'm using:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
In my project.
thanks,
matt