Hi,
I'm new to a system which has performance problems.
We are using Hibernate with JPA 1.0.
I just noticed we use Hibernate Validation (version 4.0.0.GA).
When googling I found a thread on this forum which had some information regarding Hibernate Validation and
performance. One thing stated in that thread was:
Quote:
Most important performance recommendation is to not throw away the ValidatorFactory. The cache works per factory. Validator instances are lightweight and can be easily re-created.
We have specified a Validation class of our own in persistence.xml like this:
Code:
<property name="javax.persistence.validation.group.pre-persist" value="se.ourcompany.dao.validation.BeanValidationEventListener"/>
<property name="javax.persistence.validation.group.pre-update" value="se.ourcompany.dao.validation.BeanValidationEventListener"/>
<property name="javax.persistence.validation.group.pre-remove" value=""/>
And code from BeanValidationEventListener:
Code:
public class BeanValidationEventListener {
private Log log = LogFactory.getLog(BeanValidationEventListener.class);
@PrePersist
@PreUpdate
@PreRemove
public void validate(Object entity) {
TraversableResolver tr = new BeanTraversableResolver();
Validator validator = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(tr).getValidator();
final Set<ConstraintViolation<Object>> constraintViolations = validator.validate(entity);
if (constraintViolations.size() > 0) {
//some logging takes place
}
}
}
Our entities do have a lot of validated fields and a complicated design with many associations.
Should we re-use our validation factory instead - instantiating it as a class member?
Like so:
Code:
..
private ValidatorContext validatorContext = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(new BeanTraversableResolver());
..
And then in the validate method:
Code:
..
Validator validator = validatorContext.getValidator();
..
When running one of our JUnit database test classes I do notice the new code is significantly faster.
/best regards, Håkan