'Unable to save an encrypted password with BCryptPassword in database

I am trying to save an encrypted password in postgreSQL database with BCrypt and I have an error. I saw the other answers on stackoverflow I tried to use those hints but I have the same error:This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Aug 25 19:37:41 EEST 2020 There was an unexpected error (type=Internal Server Error, status=500). Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction com.sun.proxy.$Proxy76.save(Unknown Source) .... at com.car.carDealer.service.UserService.saveUser(UserService.java:38) at com.car.carDealer.controller.Register.register(Register.java:46)

My code is:

    > > @Configuration 
         public class WebConfig implements WebMvcConfigurer {
    >     @Bean
    >     public BCryptPasswordEncoder passwordEncoder() {
    >         return new BCryptPasswordEncoder();
    >     } 
}
public interface UserRepository extends CrudRepository<User,Integer> {
        User findByEmail(String email);
        User findByEmailAndPassword(String email,String password);
}
@Service
public class UserService {

    @Autowired
    private final UserRepository userRep;

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    BCryptPasswordEncoder bCryptEncoder;

    @Autowired
    public UserService(UserRepository userRep) {
        this.userRep = userRep;
    }

    public User findByEmailAndPassword(String email, String password) {
        return userRep.findByEmailAndPassword(email, password);
    }

    public User findByEmail(String email) {
        return userRep.findByEmail(email);
    }

    public void saveUser(User user) {
        userRep.save(user);
    }
 @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(
            @Valid User user, BindingResult result, Model model) {
    @PostMapping("/register")
    public String register(@Valid User user, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "user/register";
        } else {

            User valid = userService.findByEmail(user.getEmail());
            if (valid == null) {
                user.setPassword(bCryptEncoder.encode(user.getPassword()));
                userService.saveUser(user);
                String msg = " succes!";
                model.addAttribute("msg", msg);
                return "user/register";
            } else {
                String msg = "Please choose another email !";
                model.addAttribute("msg", msg);


Solution 1:[1]

I've tried it out. Once I remove the validation it ecrypts the password & store it in database. But when I put validations like: password must be 8 to 15 characters, and Even I fulfil this condition but it does not store it in data and gives this error

Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Muneer Ahmed