Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'

不问归期 提交于 2019-11-28 10:50:17

I got the same problem, but after hours looking for the answer, Finally I Found it.... You should edit your AbstractFacade.java class and add this code

public void create(T entity) {

    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<T>> constraintViolations = validator.validate(entity);
    if(constraintViolations.size() > 0){
        Iterator<ConstraintViolation<T>> iterator = constraintViolations.iterator();
        while(iterator.hasNext()){
            ConstraintViolation<T> cv = iterator.next();
            System.err.println(cv.getRootBeanClass().getName()+"."+cv.getPropertyPath() + " " +cv.getMessage());

            JsfUtil.addErrorMessage(cv.getRootBeanClass().getSimpleName()+"."+cv.getPropertyPath() + " " +cv.getMessage());
        }
    }else{
        getEntityManager().persist(entity);
    }
}

Now this method will alert you which property and why it fails the validation. I hope this works for you, as it does for me.

I got a shortcut way,Catch the following exception where you persisting the entity. In my case its in the EJB add method. where I am doing em.persist(). Then check the server log, you will see which attribute having constrain violation.

catch (ConstraintViolationException e) {
       log.log(Level.SEVERE,"Exception: ");
       e.getConstraintViolations().forEach(err->log.log(Level.SEVERE,err.toString()));
    }

The error displays that the entity you are trying to persist is failing database constraints, so try determining the exact values you are inserting into the database before you actually insert.

and try out by commenting/ommitting @NotNull annotation also.

What I did to solve my problem was to invert the order @Size and @NotNull

before:

@Size(min=6,max=16, message="senha com no mínimo: 6 dígitos e no máximo 16 dígitos")
@NotNull(message="informe sua senha")
private String password;

after:

@NotNull(message="informe sua senha")
@Size(min=6,max=16, message="senha com no mínimo: 6 dígitos e no máximo 16 dígitos")
private String password;

I don't know why this order matter this much, but it does =] Thank you everyone!

Of course, Iomanip's answer is completely right! I've just extended it a little bit. Maybe this helps too:

  private boolean constraintValidationsDetected(T entity) {
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<T>> constraintViolations = validator.validate(entity);
    if (constraintViolations.size() > 0) {
      Iterator<ConstraintViolation<T>> iterator = constraintViolations.iterator();
      while (iterator.hasNext()) {
        ConstraintViolation<T> cv = iterator.next();
        System.err.println(cv.getRootBeanClass().getName() + "." + cv.getPropertyPath() + " " + cv.getMessage());

        JsfUtil.addErrorMessage(cv.getRootBeanClass().getSimpleName() + "." + cv.getPropertyPath() + " " + cv.getMessage());
      }
      return true;
    }
    else {
      return false;
    }
  }

  public void create(T entity) {
    if (!constraintValidationsDetected(entity)) {
      getEntityManager().persist(entity);
    }
  }

  public T edit(T entity) {
    if (!constraintValidationsDetected(entity)) {
      return getEntityManager().merge(entity);
    }
    else {
      return entity;
    }
  }

  public void remove(T entity) {
    if (!constraintValidationsDetected(entity)) {
      getEntityManager().remove(getEntityManager().merge(entity));
    }
  }

I had similar issue. In my case the sizes of referenced PK and the FK differed(See example).

Entity A:

some_pk   INTEGER NOT NULL,
fk_b      VARCHAR2(5 CHAR)
...

fk_b referenced ID field of Entity B

Entity B:

id VARCHAR2(4 CHAR)
...

id was here a PK.

Note the sizes of ID(4) and FK_B(5). After changing ID of B to 5 CHAR the error has gone.

I have spent a few hours struggling with this error while I was trying to persist an entity.

Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'preUpdate'. Please refer to embedded ConstraintViolations for details.

This error message is not helpful, quite confusing...

Anyways in my case the cause of the error was - a difference between annotation in Enity

@Size(min = 1, max = 10)
@Column(name = Utilizator.JPA_DISCRIMINATOR)
private String jpaDiscriminator;

And the DataBase column definition:

jpaDiscriminator VARCHAR(15) NOT NULL,

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!