Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a hibernate constraint individually?

Is it possible to check a String against a hibernate constraint without having to write a special Class for it? For example, if I just want to check if a given String is an email, is it possible to check it as a one-off test without writing a full class and validating that constraint against a particular property of the class?

like image 486
Ali Avatar asked Mar 06 '14 00:03

Ali


People also ask

Is Hibernate Validator deprecated?

Note that since Hibernate 5, Hibernate deprecated several of its specific validation constraint classes in favor to standard JSR 380-bean validation classes. Note also that the Hibernate Validator 6 library is actually the reference implementation of this JSR.

Is Hibernate Validator thread safe?

Validating constraints In the setUp() method, a Validator instance is retrieved from the ValidatorFactory . Validator instances are thread-safe and may be reused multiple times.

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.


1 Answers

You can annotate the property with @Email in your entity class. You can read the documentation in this link: http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/api/org/hibernate/validator/Email.html

Edited:

String email = "[email protected]";
EmailValidator validator = new EmailValidator();
validator.isValid(email, null);

The method "isValid" will return true if your string is a well-formed email address.

Hope it helps!

like image 84
Thiago Cardoso Avatar answered Sep 20 '22 03:09

Thiago Cardoso