@Autowired bean null in ConstraintValidator when invoked by Sessionfactory.getCurrentSession.merge

后端 未结 2 1615
星月不相逢
星月不相逢 2021-02-11 08:48

I have just implemented Bean Validation with Hibernate.

If I call the validator explicitly it works as expected and my @Autowired DAO bean that connects to the DB is inj

2条回答
  •  醉酒成梦
    2021-02-11 09:10

    Ok after about 18hrs of googling I finally came across a site that both described the problem it has a solution. Recipe: Using Hibernate event-based validation with custom JSR-303 validators and Spring autowired injection

    In my current project, we wanted to build a custom validator that would check if an e-mail address already existed in the database before saving any instance of our Contact entity using Hibernate. This validator needed a DAO to be injected in order to check for the existence of the e-mail address in the database. To our surprise, what we thought would be a breeze was more of a gale. Spring’s injection did not work at all when our bean was validated in the context of Hibernate’s event-based validation (in our case, the pre-insert event).

    In the end my spring configuration ended up looking like this:

    ...
    
    
        
        
    
    
    ...
    
    
    
    
        
        
        
        
            
                
                
            
        
    
    

提交回复
热议问题