Hibernate.orgCommunity Documentation
Have you ever caught yourself by unintentionally doing things like
@Past
)Then the Hibernate Validator Annotation Processor is the right thing for you. It helps preventing such mistakes by plugging into the build process and raising compilation errors whenever constraint annotations are incorrectly used.
You can find the Hibernate Validator Annotation Processor as part of the distribution bundle on
Sourceforge or in the
usual Maven repositories such as Maven Central under the GAV org.hibernate:hibernate-validator-
annotation-processor:5.2.5.Final
.
The Hibernate Validator Annotation Processor is based on the "Pluggable Annotation Processing API" as defined by JSR 269 which is part of the Java Platform since Java 6.
As of Hibernate Validator 5.2.5.Final the Hibernate Validator Annotation Processor checks that:
@Valid
The behavior of the Hibernate Validator Annotation Processor can be controlled using the processor options listed in table Table 12.1, “Hibernate Validator Annotation Processor options”:
Table 12.1. Hibernate Validator Annotation Processor options
Option | Explanation |
---|---|
| Controls how constraint problems are reported. Must be the
string representation of one of the values from the enum |
| Controls whether constraints are allowed at methods of any
kind. Must be set to |
| Controls whether detailed processing information shall be
displayed or not, useful for debugging purposes. Must be either
|
This section shows in detail how to integrate the Hibernate Validator Annotation Processor into command line builds (javac, Ant, Maven) as well as IDE-based builds (Eclipse, IntelliJ IDEA, NetBeans).
When compiling on the command line using javac, specify the JAR hibernate-validator-annotation-processor-5.2.5.Final.jar using the "processorpath" option as shown in the following listing. The processor will be detected automatically by the compiler and invoked during compilation.
Example 12.1. Using the annotation processor with javac
javac src/main/java/org/hibernate/validator/ap/demo/Car.java \ -cp /path/to/validation-api-1.1.0.Final.jar \ -processorpath /path/to/hibernate-validator-annotation-processor-5.2.5.Final.jar
Similar to directly working with javac, the annotation processor can be added as as compiler argument when invoking the javac task for Apache Ant:
Example 12.2. Using the annotation processor with An
<javac srcdir="src/main"
destdir="build/classes"
classpath="/path/to/validation-api-1.1.0.Final.jar">
<compilerarg value="-processorpath" />
<compilerarg value="/path/to/hibernate-validator-annotation-processor-5.2.5.Final.jar"/>
</javac>
There are several options for integrating the annotation processor with Apache Maven. Generally it is sufficient to add the Hibernate Validator Annotation Processor as dependency to your project:
Example 12.3. Adding the HV Annotation Processor as dependency
...
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>5.2.5.Final</version>
</dependency>
...
The processor will then be executed automatically by the compiler. This basically works, but comes with the disadavantage that in some cases messages from the annotation processor are not displayed (see MCOMPILER-66).
Another option is using the Maven Annotation Plugin. To work with this plugin, disable the standard annotation processing performed by the compiler plugin and configure the annotation plugin by specifying an execution and adding the Hibernate Validator Annotation Processor as plugin dependency (that way the processor is not visible on the project’s actual classpath):
Example 12.4. Configuring the Maven Annotation Plugin
...
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>process-sources</phase>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>5.2.5.Final</version>
</dependency>
</dependencies>
</plugin>
...
Do the following to use the annotation processor within the Eclipse IDE:
You now should see any annotation problems as regular error markers within the editor and in the "Problem" view:
The following steps must be followed to use the annotation processor within IntelliJ IDEA (version 9 and above):
Rebuilding your project then should show any erronous constraint annotations:
Starting with version 6.9, also the NetBeans IDE supports using annotation processors within the IDE build. To do so, do the following:
Any constraint annotation problems will then be marked directly within the editor:
The following known issues exist as of May 2010: