Hibernate Validator FAQ
Hibernate Validator FAQ
You’ve got burning question? We might have the answer for you.
General
What’s the difference between Hibernate Validator 3, 4 and 5?
Hibernate Validator 3.x and 4.x/5.x are different codebases.
Hibernate Validator is the original validation framework from the Hibernate team and is now referred to as "Legacy Hibernate Validator". Hibernate Validator 4.x is the reference implementation of Bean Validation 1.0 (JSR 303), while Hibernate Validator 5.x is the reference implementation of Bean Validation 1.1 (JSR 349). Active development happens on the 5.x codebase.
My question is not answered. Where can I find help?
Check out the reference guide which covers in detail all the ins and outs of Hibernate Validator. If the guide doesn’t answer your question, come to the forum and you will be helped there.
Compatibility
Does Hibernate Validator 5.x work with Tomcat 6?
It does, if you update Tomcat’s EL libraries.
Hibernate Validator 5 requires the Unified Expression Language (EL) in version 2.2 or later. While Tomcat 7 provides EL 2.2 out of the box, Tomcat 6 only comes with an EL 2.1 implementation which does not work with Hibernate Validator. You will get:
NoSuchMethodError: javax.el.ExpressionFactory.newInstance()Ljavax/el/ExpressionFactory)
You can therefore either upgrade to Tomcat 7 or update the EL libs in your Tomcat 6 installation. To do the latter, replace the JAR files el-api.jar
and and jasper-el.jar
in $CATALINE_HOME/lib
with an EL 2.2 implementation, e.g. from Tomcat 7.
Note that it is not sufficient to add an EL 2.2 implementation to your WAR and keep Tomcat’s own EL 2.1 implementation. In this case both versions will be in conflict, typically indicated by a LinkageError saying that the ExpressionFactory class has been loaded twice by different class loaders.
Constraints
How do I check that two fields of my bean have the same value?
By implementing a class-level constraint.
How can I access the Hibernate Session or EntityManager within a ConstraintValidator?
See this example.
Is it possible to determine whether a given constraint is specified at field or property level using the metadata API?
Yes.
PropertyDescriptor property =
validator.getConstraintsForClass(Address.class)
.getConstraintsForProperty("street1");
Set<ConstraintDescriptor<?>> fieldConstraints =
property
.findConstraints()
.lookingAt(Scope.LOCAL_ELEMENT)
.declaredOn(ElementType.FIELD)
.getConstraintDescriptors();
Set<ConstraintDescriptor<?>> propertyConstraints =
property
.findConstraints()
.lookingAt(Scope.LOCAL_ELEMENT)
.declaredOn(ElementType.METHOD)
.getConstraintDescriptors();
The key is the use of the +findConstraints()+ fluent API. You have three ways to restrict the metadata:
-
declaredOn(ElementType… types)
: defines where to look the constraints (METHOD, FIELD etc) -
lookingAt(Scope scope)
: defines whether to look for constraints hosted on superclass/interfaces or not -
unorderedAndMatchingGroups(Class<?>… groups)
: restrict to the constraints matching a given set of groups for this element
Errors
When using the HV annotation processor, I’m getting a compilation error like "Incompatible types, found: XY, required XY" - what’s wrong?
This is caused by a bug in the javac compiler and occurs when the compiled project contains an annotation type with an enum-typed member which has a default value. As workaround either specify the default value using its fully qualified name or add this annotation processor to your project. See also HV-498 for more details.