Spring + Hibernate @autowired is null in ConstraintValidator

前端 未结 1 1541
一整个雨季
一整个雨季 2021-01-20 15:58

I have search and found a lot of topic on this issue, but for whatever reason the solutions does not solve my problem.

I am running Spring boot 1.5.8 and Hibernate.<

相关标签:
1条回答
  • 2021-01-20 16:15

    Please see below answers, I could reproduce this in my environment as well.

    The validator being called twice, once the request is posted, another one is after the validation is done and entity gets persisted to DB, for the second time the autowired bean becomes null and throws NPE.

    From the below answers, set validator property in entity manager along with other properties

    <bean
        id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceProvider">
          <bean class="org.hibernate.ejb.HibernatePersistence" />
        </property>
            <property name="jpaPropertyMap">
          <map>  
            <entry key="javax.persistence.validation.factory" value-ref="validator" />
          </map>
        </property>
    </bean>
    
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
    

    http://forum.spring.io/forum/spring-projects/web/84838-custom-jsr-303-validator-firing-twice

    https://developer.jboss.org/thread/139770

    How to inject spring bean into Validator(hibernate)

    https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/additionalmodules.html

    0 讨论(0)
提交回复
热议问题