Customize error message

3

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

  

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

It is a constraint violation. How can I customize to display an example persoanlized message: Warning: 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 in advance.

    
asked by anonymous 09.06.2016 / 22:20

2 answers

3

You have to handle this exception.

try {
    cadastrar();
} catch (ConstraintViolationException e) {
    erro("Este nome já existe no banco de dados");
    // e.printStackTrace();
}

Read about Exceptions, because that's what you need. This link can help.

    
10.06.2016 / 20:29
5

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

    } catch (ConstraintViolationException e) {
        e.printStackTrace();
    }
    
09.06.2016 / 22:40