saving method: org.hibernate.exception.ConstraintViolationException: could not execute statement

1

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

The problem is that in the Physician it generates the error: Column 'activity_code' can not be null and in Legal generates the error: Column 'data_nation' can not be null.

NOTE: When I do not render the xhtml it saves to 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();
}
    
asked by anonymous 21.08.2016 / 00:53

1 answer

0

I discovered my primary error, the problem is that in the database the field date_birth and branch_activity were not null and in my implementation I was using 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.

    
24.08.2016 / 14:22