Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a Hibernate validator without an annotation on a field?

Tags:

hibernate

Is it possible to use the Hibernate validator API storing the validated object in a field with an annotation? For example, I would like to validate Email address by calling a Java method instead of putting an @Email annotation on a Java bean property.

like image 961
EeE Avatar asked Dec 26 '10 07:12

EeE


People also ask

How does Hibernate Validator work?

Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming.

What is the difference between javax validation and Hibernate Validator?

The Javax bean validation API provides the following most frequently used annotations. The Hibernate validator provides the following commonly used annotations for validation. In case of product or project development we must use both the annotations for bean validation.

What is Hibernate Validator annotation processor?

Hibernate Validator Annotation Processor It helps to detect glitches such as: Putting a constraint to bean properties which are not supported by that constraint (e.g. putting @Date to a String) Annotating the setter of a bean property instead of the getter. Adding constraints to static fields or methods.

What interface must you implement when you require class level validation?

The validation class implements the ConstraintValidator interface, and must also implement the isValid method; it's in this method that we defined our validation rules.


1 Answers

Yes, it is possible. Try Something like,

Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

Car car = new Car(null);

Set<ConstraintViolation<Car>> constraintViolations =
    validator.validateProperty(car, "manufacturer");

Take a look at here.

like image 50
zinan.yumak Avatar answered Sep 22 '22 11:09

zinan.yumak