method save: org.hibernate.Exception.Constraintviolationexception: could not execute statement

Asked

Viewed 1,707 times

1

Guys, I have a little screen that registers name and type. When I click type Physics is rendered the date of birth and I need to save it and when I click Legal I have to save a branch of activity that is loading the branches correctly in xhtml.

The problem is that in Physical it generates the error: Column 'codigo_ramo_activity' cannot be null and in Legal it generates the error: Column 'data_nascimento' cannot be null.

NOTE: When I do not rewrite the xhtml it saves in the database correctly.

<h:outputLabel value="Tipo:" for="tipo" />
            <h:selectOneRadio id="tipo" value="#{cadastroPessoaBean.pessoa.tipo}" 
                required="true" label="Tipo pessoa" immediate="true"
                valueChangeListener="#{cadastroPessoaBean.tipoPessoaAlterado}"
                onchange="submit();">
                <f:selectItems value="#{cadastroPessoaBean.pessoas}" var="pessoa"
                    itemLabel="#{pessoa.descricao}" itemValue="#{pessoa}" />
            </h:selectOneRadio>

<h:outputLabel value="Data de nascimento:" rendered="#{cadastroPessoaBean.pessoa.tipo eq 'FISICA'}" />
            <h:inputText size="12" value="#{cadastroPessoaBean.pessoa.dataNascimento}"
                required="true" label="Data de nascimento" rendered="#{cadastroPessoaBean.pessoa.tipo eq 'FISICA'}">
                <f:convertDateTime pattern="dd/MM/yyyy"/>
            </h:inputText>

            <h:outputLabel value="Ramo de atividade:" rendered="#{cadastroPessoaBean.pessoa.tipo eq 'JURIDICA'}"/>
            <h:selectOneMenu value="#{cadastroPessoaBean.pessoa.ramoAtividade}" required="true"
                label="Ramo de atividade" rendered="#{cadastroPessoaBean.pessoa.tipo eq 'JURIDICA'}">
                <f:selectItem itemLabel=":: Selecione ::" noSelectionOption="true"/>
                <f:selectItems value="#{cadastroPessoaBean.ramosAtividades}" var="ramoAtividade"
                    itemLabel="#{ramoAtividade.descricao}" itemValue="#{ramoAtividade}"/>
            </h:selectOneMenu>

Bean:

public void cadastrar() {
    Session session = (Session) FacesUtil.getRequestAttribute("session");

    session.merge(this.pessoa);

    this.pessoa = new Pessoa();

public void tipoPessoaAlterado(ValueChangeEvent event) {
    this.pessoa.setTipo((TipoPessoa) event.getNewValue());
    this.pessoa.setDataNascimento(null);
    this.pessoa.setRamoAtividade(null); //new RamoAtividade()

    FacesContext.getCurrentInstance().renderResponse();
}

1 answer

0


I discovered my primary error, the problem is that in the database the field data_birth and activity were with not null and in my implementation was using the Restrictions eq.

Solution: In the database, in the person table it was necessary to define these two fields as not required (NOT NULL = FALSE) and in the implementation instead of eq I changed to Restrictions.eqOrIsNull.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.