@Autowired bean works with @Valid on controller but fails with CRUD repository

Viewed 3325

I'm working on a Spring MVC + Hibernate + JPA app with a user registration form and I decided to use a JSR-303 validator to check whether the username already existed in the DB:

public class UniqueUsernameValidator implements ConstraintValidator<VerifyUniqueUsername, String> {

    @Autowired
    UserService userService;

    @Override
    public void initialize(VerifyUniqueUsername constraintAnnotation) {     
    }

    @Override
    public boolean isValid(String username, ConstraintValidatorContext context) {                       

        return  username!=null &&  userService.findByUsername(username) == null;        
    }
}

It is very straightforward and validation worked great on my controller:

....
    public String signup(@Valid @ModelAttribute("newUser") User user, BindingResult newUserBeanResult)
.....

The current problem I'm facing is that after I get my User object validated and I call:

userService.save(user);

Which implements CrudRepository, I get a NullPointerException. For some reason UserService is injected during validation on the controller but not when I call CrudRepository.save().

I saw similar posts such as this: @Autowired bean null in ConstraintValidator when invoked by Sessionfactory.getCurrentSession.merge and this: hibernate validator without using autowire but I was wondering if someone has run into this before. I would think that injecting beans to access the database on a validator is fairly common.

As a workaround I added a check for null on userService but it doesn't feel right.

  1. Is this expected behaviour? Are these validations supossed to fire before calling CrudRepository.save()?
  2. Am I suppossed to handle "manually" hibernate events? In this case pre-insert
2 Answers
Related