JSR-303 dependency injection and Hibernate

Posted by Jam on Stack Overflow See other posts from Stack Overflow or by Jam
Published on 2010-04-26T09:34:46Z Indexed on 2010/04/28 11:03 UTC
Read the original article Hit count: 272

Spring 3.0.2, Hibernate 3.5.0, Hibernate-Validator 4.0.2.GA

I am trying to inject Spring dependencies into a ConstraintValidator using:

@PersistenceContext
private EntityManager entityManager;

I have configured the application context with:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

Which, according to the Spring documentation, should allow “custom ConstraintValidators to benefit from dependency injection like any other Spring bean”

Within the debugger I can see Spring calling getBean to create the ConstraintValidator. Later when flush triggers the preInsert, a different ConstraintValidator is created and called. The problem is the EntityManager is null within this new ConstraintValidator. I’ve tried injecting other dependencies within the ConstraintValidator and these are always null.

Does anyone know if it is possible to inject dependencies into a ConstraintValidator?

© Stack Overflow or respective owner

Related posts about spring

Related posts about hibernate