I am using javax.validation.Validation to validate jpa entities. I am always validating against the same Entities.
I would like to know if it is better to use one Validator for all validations or to instantiate a new Validator each time I validate.
As well, how expensive in terms of computation is it for me to instantiate a new validator each time I would like to use it?
Option1: instantiate new validator for each validation.
public class Validator
{
public static void main(String[] args)
{
//Validation1
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Entity entityToValidate = new Entity();
entityToValidate.setEmail("NOT_A_VALID_EMAIL@@@tomanyat.com");
Set<ConstraintViolation<T>> constraintViolations = validator.validate(entityToValidate);
//Validation2 (Note that validator has been created yet a second time !! )
Validator validator2 = factory.getValidator();
Entity entityToValidate2 = new Entity();
entityToValidate.setEmail("NOT_A_VALID_EMAIL@@@tomanyat.com");
Set<ConstraintViolation<T>> constraintViolations2 = validator2.validate(entityToValidate);
}
}
Option2: single validator for all validations.
public class Validator
{
public static void main(String[] args)
{
//Validator created only once
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
//Validation #1
Entity entityToValidate = new Entity();
entityToValidate.setEmail("NOT_A_VALID_EMAIL@@@tomanyat.com");
Set<ConstraintViolation<T>> constraintViolations = validator.validate(entityToValidate);
//Validation #2
Entity entityToValidate2 = new Entity();
entityToValidate.setEmail("NOT_A_VALID_EMAIL@@@tomanyat.com");
Set<ConstraintViolation<T>> constraintViolations2 = validator .validate(entityToValidate);
}
}
The ValidatorFactory object built by the bootstrap process should be cached and shared amongst Validator consumers. This class is thread-safe.
@Validated annotation is a class-level annotation that we can use to tell Spring to validate parameters that are passed into a method of the annotated class. @Valid annotation on method parameters and fields to tell Spring that we want a method parameter or field to be validated. Hope this helps.
validation will validate the nested object for constraints with the help of javax. validation implementation provider, for example, hibernate validator. @Valid also works for collection-typed fields. In the case of collection-typed fields, each collection element will be validated.
In the setUp() method, a Validator instance is retrieved from the ValidatorFactory . Validator instances are thread-safe and may be reused multiple times.
Note how the Validator
javadoc states
Validates bean instances. Implementations of this interface must be thread-safe.
As such a Validator
shouldn't really contain state, unless that state is also thread-safe. Therefore, you shouldn't need to create a new Validator
instance, just re-use the same one, depending on the types obviously.
As well, how expensive in terms of computation is it for me to instantiate a new validator each time I would like to use it?
This depends on your Validator
. But the instantiation (creating but not initializing the object) itself is almost completely negligible, especially when you consider all the processing that a JPA implementation performs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With