java – Customize error message


I forced an error on my system that contains the following message:

javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement

It's a constraint violation. How can I customize to display a custom message example: Attention: This name already exists in the database.

I even created a class for Messages and it has this method:

public static void erro(String mensagem) {

        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, mensagem, "");
        FacesContext.getCurrentInstance().addMessage("messagePanel", msg);


Thank you very much in advance.


You can catch the Exception ConstraintViolationException in a catch and customize the return of a message for that error.

    } catch (ConstraintViolationException e) {
Scroll to Top