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

0

Error trying to persist entities in JPA

  

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

    
asked by Next Dev 19.05.2017 в 01:17
source

2 answers

0

This error occurs when the constraints defined in the entity that are persiste are violated. You must validate that the values that are NotNull are not saved with null and any other defined restriction.

You could use the following code to clarify a little more the error about what has been the problem.

I use the AbstracFacade that is generated in NetBeans, and within this file, in the methods create and edit , define them as follows:

public void create(T entity) {
    try {
        getEntityManager().persist(entity);
    } catch (ConstraintViolationException e) {
        // Aqui tira los errores de constraint
        for (ConstraintViolation actual : e.getConstraintViolations()) {
            System.out.println(actual.toString());
        }
    }
}

public T edit(T entity) {
    try {
        return getEntityManager().merge(entity);
    } catch (ConstraintViolationException e) {
        // Aqui tira los errores de constraint
        for (ConstraintViolation actual : e.getConstraintViolations()) {
            System.out.println(actual.toString());
        }
    }

    return null;
}

Thus, at the time that the persistence error occurs, in the log it will show you more detail of the error.

    
answered by 19.05.2017 / 18:43
source
0

I have exactly the same error. I am migrating a java application project to a web application with maven. It all has to do with the persistence.xml file.

In Validation Strategy you must enter None.

    
answered by 20.03.2018 в 22:18