Automatic validation is achieved through Java API for bean validation using annotations such as @NotNull, @Min,@Max, @Size etc.
To run with Hibernate Validator 4.x, just hibernate is enough:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.2.0.Final</version> </dependency>
To run with Hibernate Validator 5.x add this dependencies:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.4.3.Final</version> </dependency> <dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>javax.el</artifactId> <version>2.2.4</version> </dependency>
To run with Hibernate Validator 6.x add this dependencies:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>6.1.2.Final</version> </dependency> <dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>javax.el</artifactId> <version>2.2.6</version> </dependency>
Note: If those libraries wasn’t configured for Validator 5.x and 6.x you are get this exception:
javax.validation.ValidationException: HV000183: Unable to initialize 'javax.el.ExpressionFactory'. Check that you have the EL dependencies on the classpath, or use ParameterMessageInterpolator instead
import net.sf.jkniv.sqlegance.validation.AddValidate; import net.sf.jkniv.sqlegance.validation.UpdateValidate; public class User { @NotNull(groups = { UpdateValidate.class, RemoveValidate.class }) private Long id; @Email(groups = { UpdateValidate.class, AddValidate.class }) private String email; @NotNull(groups = { UpdateValidate.class }) private Date lastUpdate; }
The @NotNull, @Email annotations are used to declare the constraints which should be applied to the fields of a User instance while AddValidate, UpdateValidate and RemoveValidate are used to say when apply the validation:
Beyond the annotations for the field is necessary specify the SQL to be validate when execute it, example:
<insert id="save-user" validation="ADD"> insert into users (email, lastUpdate) values (:email, :lastUpdate) </insert> <update id="update-user" validation="UPDATE"> update users set email = :email, lastUpdate = :lastUpdate where id = :id </update>
The validations are applied before execute the methods from Repository: