title: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
body: <p>I have a error when I try to insert a user in the database.</p>
  
  <p>I made i custom annotation which verify if the password match with the confirmation password it works when the field not matches , but when the passowrd matches i have this error :</p>
  
  <p><a href="https://i.stack.imgur.com/Zfcdd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Zfcdd.jpg" alt="enter image description here"></a> </p>
  
  <p>This is my code This is my field match @Annotation :</p>
  
  <pre><code>package mereuta.marian.tennis01.annotations;
  
  
  import javax.validation.Constraint;
  import javax.validation.Payload;
  import java.lang.annotation.ElementType;
  import java.lang.annotation.Retention;
  import java.lang.annotation.RetentionPolicy;
  import java.lang.annotation.Target;
  
  @Constraint(validatedBy = FieldsValueMatchValidator.class)
  @Target({ ElementType.TYPE })
  @Retention(RetentionPolicy.RUNTIME)
  public @interface FieldsValueMatch {
  
      String message() default "Fields values don't match!";
  
      String field();
  
      String fieldMatch();
  
      Class&lt;?&gt;[] groups() default {};
      Class&lt;? extends Payload&gt;[] payload() default {};
  
      @Target({ ElementType.TYPE })
      @Retention(RetentionPolicy.RUNTIME)
      @interface List {
          FieldsValueMatch[] value();
      }
  }
  </code></pre>
  
  <p>This is the Field Validator :</p>
  
  <pre><code>package mereuta.marian.tennis01.annotations;
  
  import mereuta.marian.tennis01.model.Utilisateur;
  import org.springframework.beans.BeanWrapperImpl;
  
  import javax.validation.ConstraintValidator;
  import javax.validation.ConstraintValidatorContext;
  
  public class FieldsValueMatchValidator implements ConstraintValidator&lt;FieldsValueMatch , Object&gt; {
  
      private String field;
      private String fieldMatch;
  
  
      @Override
      public void initialize(FieldsValueMatch fieldsValueMatch) {
          this.field=fieldsValueMatch.field();
          this.fieldMatch=fieldsValueMatch.fieldMatch();
      }
  
      @Override
      public boolean isValid(Object value, ConstraintValidatorContext context) {
  
  
          Object fieldValue = new BeanWrapperImpl(value)
                  .getPropertyValue(field);
          Object fieldMatchValue = new BeanWrapperImpl(value)
                  .getPropertyValue(fieldMatch);
  
          if (fieldValue != null) {
              return fieldValue.equals(fieldMatchValue);
          } else {
              return fieldMatchValue == null;
          }
      }
  }
  </code></pre>
  
  <p>This is my Entity :</p>
  
  <pre><code>@FieldsValueMatch(field = "password", fieldMatch = "confirmPassword", 
  message = "Password do not match!")
  @Entity(name = "utilisateurs")
  public class Utilisateur {
      @Id @GeneratedValue
      @Column(name = "id_utilisateur")
      private Integer id;
      @NotNull
      @Size(min = 4, max = 255)
      @Column(name = "password")
      private String password;
      @Transient
      @NotNull
      private String confirmPassword;
  </code></pre>
  
  <p>This is the Controller :</p>
  
  <pre><code>@PostMapping("/addUtilisateur")
      public String addUtilisateur(@Valid @ModelAttribute("utilisateur") Utilisateur utilisateur, BindingResult bindingResult, Model model) {
  
  
          if (bindingResult.hasErrors() ) {
              model.addAttribute("message", "le mot de passe ne correspond pas");
              return "utilisateur/formRegister";
          }
  
  
          utilisateurMetier.creerUtilisateur(utilisateur);
  
          return "utilisateur/utilisateurAjoute";
  
  
      }
  </code></pre>
  
  <p>And finally the View : </p>
  
  <pre><code>&lt;div class="container"&gt;
          &lt;form id="contact" th:action="@{addUtilisateur}" method="post" th:object="${utilisateur}"&gt;
              &lt;h3&gt;Créer compte&lt;/h3&gt;
  
  
                  &lt;input placeholder="password" type="password" th:field="*{password}" tabindex="2" required/&gt;
                  &lt;span class="text text-danger" th:if="${#fields.hasErrors('password')}" th:errors="*{password}"&gt;&lt;/span&gt;
  
              &lt;/fieldset&gt;
  
  
              &lt;fieldset&gt;
  
                  &lt;input placeholder="password" type="password" th:field="*{confirmPassword}" tabindex="2" required/&gt;
                  &lt;span class="text text-danger" th:if="${#fields.hasErrors('confirmPassword')}"
                        th:errors="*{confirmPassword}" th:text="${message}"&gt;&lt;/span&gt;
  
  
              &lt;/fieldset&gt;
  </code></pre>
  
  <p>For the custom annotations I find a example on : <a href="https://www.baeldung.com/spring-mvc-custom-validator" rel="nofollow noreferrer">https://www.baeldung.com/spring-mvc-custom-validator</a></p>
  
  <pre><code>@Override
      public void creerUtilisateur(Utilisateur utilisateur) {
  
          Role role;
          float credit = 0;
          boolean actif = true;
  
          role = roleRepository.getOne(3);
  
  
          System.out.println(role);
  
          BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
  
  
          utilisateur.setPassword(encoder.encode(utilisateur.getPassword()));
  
          utilisateur.setRole(role);
          utilisateur.setCredit(credit);
          utilisateur.setActif(actif);
  
  
          utilisateurRepository.save(utilisateur);
      }
  </code></pre>
  
  <p>Thank you in advance for your help </p>