Error while doing Hibernate relationship with Java

Asked

Viewed 162 times

0

Good afternoon guys! I have a question in Hibernate. I have the following case Person - Client - Supplier

But I’m having trouble making the relationship. follows my codes

New Error

18:50:34,405 DEBUG [org.hibernate.SQL] - 
    select
        SEQ_CLIENTE.nextval 
    from
        dual
Hibernate: 
    select
        SEQ_CLIENTE.nextval 
    from
        dual
18:50:34,534 DEBUG [org.hibernate.SQL] - 
    select
        SEQ_PESSOA.nextval 
    from
        dual
Hibernate: 
    select
        SEQ_PESSOA.nextval 
    from
        dual
18:50:34,630 DEBUG [org.hibernate.SQL] - 
    /* insert model.PessoaModel
        */ insert 
        into
            PESSOA
            (PES_ATIVO, PES_BAIRRO, PES_CADASTRO, PES_CELULAR, PES_CEP, PES_CIDADE, PES_COMPLEMENTO, PES_CPFCNPJ, PES_EMAIL, PES_ENDERECO, PES_FANTASIA, PES_FISICA, PES_FONE1, PES_FONE2, PES_NOME, PES_NUMERO, PES_RGIE, PES_SITE, PES_UF, PES_CODIGO) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    /* insert model.PessoaModel
        */ insert 
        into
            PESSOA
            (PES_ATIVO, PES_BAIRRO, PES_CADASTRO, PES_CELULAR, PES_CEP, PES_CIDADE, PES_COMPLEMENTO, PES_CPFCNPJ, PES_EMAIL, PES_ENDERECO, PES_FANTASIA, PES_FISICA, PES_FONE1, PES_FONE2, PES_NOME, PES_NUMERO, PES_RGIE, PES_SITE, PES_UF, PES_CODIGO) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
18:50:34,631 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [1] as [CHAR] - [S]
18:50:34,631 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [2] as [VARCHAR] - [1]
18:50:34,634 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [3] as [TIMESTAMP] - [Thu Nov 01 18:50:31 BRT 2018]
18:50:34,634 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [4] as [VARCHAR] - [1]
18:50:34,634 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [5] as [VARCHAR] - [1]
18:50:34,635 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [6] as [VARCHAR] - [1]
18:50:34,635 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [7] as [VARCHAR] - [1]
18:50:34,635 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [8] as [VARCHAR] - [1]
18:50:34,635 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [9] as [VARCHAR] - [1]
18:50:34,635 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [10] as [VARCHAR] - [1]
18:50:34,635 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [11] as [VARCHAR] - [1]
18:50:34,635 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [12] as [CHAR] - [S]
18:50:34,635 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [13] as [VARCHAR] - [1]
18:50:34,635 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [14] as [VARCHAR] - [1]
18:50:34,635 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [15] as [VARCHAR] - [1]
18:50:34,636 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [16] as [VARCHAR] - [1]
18:50:34,636 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [17] as [VARCHAR] - [1]
18:50:34,636 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [18] as [VARCHAR] - [1]
18:50:34,636 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [19] as [VARCHAR] - [1]
18:50:34,636 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [20] as [INTEGER] - [35]
18:50:34,921 DEBUG [org.hibernate.SQL] - 
    /* insert model.ClienteModel
        */ insert 
        into
            CLIENTE
            (CLI_LIMITECRED, PES_CODIGO, CLI_CODIGO) 
        values
            (?, ?, ?)
Hibernate: 
    /* insert model.ClienteModel
        */ insert 
        into
            CLIENTE
            (CLI_LIMITECRED, PES_CODIGO, CLI_CODIGO) 
        values
            (?, ?, ?)
18:50:34,922 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [1] as [DOUBLE] - [1.0]
18:50:34,923 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [2] as [INTEGER] - [35]
18:50:34,923 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [3] as [INTEGER] - [34]
18:50:35,141 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] - SQL Error: 2291, SQLState: 23000
18:50:35,141 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] - ORA-02291: restrição de integridade (UNIP.FK_CLIENTE) violada - chave mãe não localizada

error

nov 01, 2018 2:24:51 PM org.hibernate.action.internal.UnresolvedEntityInsertActions logCannotResolveNonNullableTransientDependencies
WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
    Unsaved transient entity: ([model.PessoaModel#0])
    Dependent entities: ([[model.ClienteModel#6]])
    Non-nullable association(s): ([model.ClienteModel.pessoamodel])

Error after a modification in Hibernate relationship Modification from EAGER to LAZY

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)

Error after modification

    Hibernate: select SEQ_CLIENTE.nextval from dual
Hibernate: select SEQ_PESSOA.nextval from dual
Hibernate: insert into PESSOA (PES_ATIVO, PES_BAIRRO, PES_CADASTRO, PES_CELULAR, PES_CEP, PES_CIDADE, PES_COMPLEMENTO, PES_CPFCNPJ, PES_EMAIL, PES_ENDERECO, PES_FANTASIA, PES_FISICA, PES_FONE1, PES_FONE2, PES_NOME, PES_NUMERO, PES_RGIE, PES_SITE, PES_UF, PES_CODIGO) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into CLIENTE (CLI_LIMITECRED, PES_CODIGO, CLI_CODIGO) values (?, ?, ?)
nov 01, 2018 2:58:30 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 2291, SQLState: 23000
nov 01, 2018 2:58:30 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-02291: restrição de integridade (UNIP.FK_CLIENTE_PESSOA) violada - chave mãe não localizada

nov 01, 2018 2:58:30 PM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]

Model Cliente

package model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "CLIENTE")
public class ClienteModel implements java.io.Serializable {


    private int CLI_CODIGO;
    private Double CLI_LIMITECRED;
    private PessoaModel pessoamodel;

    @Id
    @Column(name = "CLI_CODIGO", unique = true, nullable = false, precision = 38, scale = 0)
    @SequenceGenerator(name = "SEQ_CLIENTE", sequenceName = "SEQ_CLIENTE", allocationSize = 0)
    @GeneratedValue(generator = "SEQ_CLIENTE", strategy = GenerationType.SEQUENCE)
    public int getCLI_CODIGO() {
        return CLI_CODIGO;
    }

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "PES_CODIGO", nullable = false)
    public PessoaModel getPessoamodel() {
        return pessoamodel;
    }

    public void setPessoamodel(PessoaModel pessoamodel) {
        this.pessoamodel = pessoamodel;
    }

    public void setCLI_CODIGO(int CLI_CODIGO) {
        this.CLI_CODIGO = CLI_CODIGO;
    }



    @Column(name = "CLI_LIMITECRED", unique = true, nullable = false, precision = 38, scale = 0)
    public Double getCLI_LIMITECRED() {
        return CLI_LIMITECRED;
    }

    public void setCLI_LIMITECRED(Double CLI_LIMITECRED) {
        this.CLI_LIMITECRED = CLI_LIMITECRED;
    }




}

Model Pessoa

    package model;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "PESSOA")
public class PessoaModel implements java.io.Serializable {
    private int PES_CODIGO;
    private String PES_NOME;
    private String PES_FANTASIA;
    private char PES_FISICA;
    private String PES_CPFCNPJ;
    private String PES_RGIE;

    private Date PES_CADASTRO = new java.sql.Date(System.currentTimeMillis());
    private String PES_ENDERECO;
    private String PES_NUMERO;
    private String PES_COMPLEMENTO;
    private String PES_BAIRRO;
    private String PES_CIDADE;
    private String PES_UF;
    private String PES_CEP;
    private String PES_FONE1;
    private String PES_FONE2;
    private String PES_CELULAR;
    private String PES_SITE;
    private String PES_EMAIL;
    private char PES_ATIVO;

    public PessoaModel() {
    }

    @Id
    @Column(name = "PES_CODIGO", unique = true, nullable = false, precision = 38, scale = 0)
    @SequenceGenerator(name = "SEQ_PESSOA", sequenceName = "SEQ_PESSOA", allocationSize = 0)
    @GeneratedValue(generator = "SEQ_PESSOA", strategy = GenerationType.SEQUENCE)

    public int getPES_CODIGO() {
        return PES_CODIGO;
    }

    public void setPES_CODIGO(int PES_CODIGO) {
        this.PES_CODIGO = PES_CODIGO;
    }

    @Column(name = "PES_NOME", length = 80)
    public String getPES_NOME() {
        return PES_NOME;
    }

    public void setPES_NOME(String PES_NOME) {
        this.PES_NOME = PES_NOME;
    }

    @Column(name = "PES_FANTASIA", length = 80)
    public String getPES_FANTASIA() {
        return PES_FANTASIA;
    }

    public void setPES_FANTASIA(String PES_FANTASIA) {
        this.PES_FANTASIA = PES_FANTASIA;
    }

    @Column(name = "PES_FISICA", length = 1)
    public char getPES_FISICA() {
        return PES_FISICA;
    }

    public void setPES_FISICA(char PES_FISICA) {
        this.PES_FISICA = PES_FISICA;
    }

    @Column(name = "PES_CPFCNPJ", length = 20)
    public String getPES_CPFCNPJ() {
        return PES_CPFCNPJ;
    }

    public void setPES_CPFCNPJ(String PES_CPFCNPJ) {
        this.PES_CPFCNPJ = PES_CPFCNPJ;
    }

    @Column(name = "PES_RGIE", length = 20)
    public String getPES_RGIE() {
        return PES_RGIE;
    }

    public void setPES_RGIE(String PES_RGIE) {
        this.PES_RGIE = PES_RGIE;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "PES_CADASTRO", nullable = false, length = 7)
    public Date getPES_CADASTRO() {
        return PES_CADASTRO;
    }

    public void setPES_CADASTRO(Date PES_CADASTRO) {
        this.PES_CADASTRO = PES_CADASTRO;
    }

    @Column(name = "PES_ENDERECO", length = 120)
    public String getPES_ENDERECO() {
        return PES_ENDERECO;
    }

    public void setPES_ENDERECO(String PES_ENDERECO) {
        this.PES_ENDERECO = PES_ENDERECO;
    }

    @Column(name = "PES_NUMERO", length = 10)
    public String getPES_NUMERO() {
        return PES_NUMERO;
    }

    public void setPES_NUMERO(String PES_NUMERO) {
        this.PES_NUMERO = PES_NUMERO;
    }

    @Column(name = "PES_COMPLEMENTO", length = 30)
    public String getPES_COMPLEMENTO() {
        return PES_COMPLEMENTO;
    }

    public void setPES_COMPLEMENTO(String PES_COMPLEMENTO) {
        this.PES_COMPLEMENTO = PES_COMPLEMENTO;
    }

    @Column(name = "PES_BAIRRO", length = 50)
    public String getPES_BAIRRO() {
        return PES_BAIRRO;
    }

    public void setPES_BAIRRO(String PES_BAIRRO) {
        this.PES_BAIRRO = PES_BAIRRO;
    }

    @Column(name = "PES_CIDADE", length = 80)
    public String getPES_CIDADE() {
        return PES_CIDADE;
    }

    public void setPES_CIDADE(String PES_CIDADE) {
        this.PES_CIDADE = PES_CIDADE;
    }

    @Column(name = "PES_UF", length = 2)
    public String getPES_UF() {
        return PES_UF;
    }

    public void setPES_UF(String PES_UF) {
        this.PES_UF = PES_UF;
    }

    @Column(name = "PES_CEP", length = 9)
    public String getPES_CEP() {
        return PES_CEP;
    }

    public void setPES_CEP(String PES_CEP) {
        this.PES_CEP = PES_CEP;
    }

    @Column(name = "PES_FONE1", length = 15)
    public String getPES_FONE1() {
        return PES_FONE1;
    }

    public void setPES_FONE1(String PES_FONE1) {
        this.PES_FONE1 = PES_FONE1;
    }

    @Column(name = "PES_FONE2", length = 15)
    public String getPES_FONE2() {
        return PES_FONE2;
    }

    public void setPES_FONE2(String PES_FONE2) {
        this.PES_FONE2 = PES_FONE2;
    }

    @Column(name = "PES_CELULAR", length = 15)
    public String getPES_CELULAR() {
        return PES_CELULAR;
    }

    public void setPES_CELULAR(String PES_CELULAR) {
        this.PES_CELULAR = PES_CELULAR;
    }

    @Column(name = "PES_SITE", length = 200)
    public String getPES_SITE() {
        return PES_SITE;
    }

    public void setPES_SITE(String PES_SITE) {
        this.PES_SITE = PES_SITE;
    }

    @Column(name = "PES_EMAIL", length = 200)
    public String getPES_EMAIL() {
        return PES_EMAIL;
    }

    public void setPES_EMAIL(String PES_EMAIL) {
        this.PES_EMAIL = PES_EMAIL;
    }

    @Column(name = "PES_ATIVO", length = 1)
    public char getPES_ATIVO() {
        return PES_ATIVO;
    }

    public void setPES_ATIVO(char PES_ATIVO) {
        this.PES_ATIVO = PES_ATIVO;
    }


}

Client Controller

package controller;

import dao.ClienteDao;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.ClienteModel;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import util.ReportUtils;

public class ClienteController implements GenericController<ClienteModel> {

    ClienteDao clientedao;

    public ClienteController() {
        clientedao = new ClienteDao();
    }

    @Override
    public void incluir(ClienteModel obj) throws Exception{
        clientedao.incluir(obj);
    }

    @Override
    public void alterar(ClienteModel obj) throws Exception{
        clientedao.alterar(obj);
    }

    @Override
    public void excluir(ClienteModel obj) throws Exception{
        clientedao.excluir(obj);
    }

    @Override
    public ArrayList<ClienteModel> consultar(String filtro) {
        return clientedao.consultar(filtro);
    }

    @Override
    public void gravar(ClienteModel obj, String operacao) throws Exception{
        if (operacao.equals("incluir")) {
            incluir(obj);
        } else {
            alterar(obj);
        }
    }

    @Override
    public Exception imprimir() {
        Exception retorno = null;
        InputStream inputStream = getClass().getResourceAsStream("/relatorios/RelatorioCompra.jasper");
//        btnPRIMEIRO.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/primeiro.png"))); // NOI18N

        Map parametros = new HashMap();

        List dados = consultar("");

        // criando o datasource com os dados criados
        JRDataSource ds = new JRBeanCollectionDataSource(dados);

        try {
            // passando o datasource para o método de criação e exibição do relatório
            ReportUtils.openReport("Compras - Bean Collection Data Source", inputStream, parametros, ds);

        } catch (Exception ex) {
            retorno =  ex;
        }
        return retorno;
    }
}

Pessoa Controller

package controller;

import dao.PessoaDao;
import dao.ClienteDao;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.PessoaModel;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import util.ReportUtils;


public class PessoaController implements GenericController<PessoaModel> {

    PessoaDao pessoadao;
    ClienteDao clientedao;

    public PessoaController() {
        pessoadao = new PessoaDao();
        clientedao = new ClienteDao();
    }



    @Override
    public void incluir(PessoaModel obj) throws Exception{

        pessoadao.incluir(obj); 
    }


    @Override
    public void alterar(PessoaModel obj) throws Exception{
        pessoadao.alterar(obj);
    }

    @Override
    public void excluir(PessoaModel obj) throws Exception{
        pessoadao.excluir(obj);
    }

    @Override
    public ArrayList<PessoaModel> consultar(String filtro) {
        return pessoadao.consultar(filtro);
    }

    @Override
    public void gravar(PessoaModel obj, String operacao) throws Exception{
        if (operacao.equals("incluir")) {
            incluir(obj);
        } else {
            alterar(obj);
        }
    }

    @Override
    public Exception imprimir() {
        Exception retorno = null;
        InputStream inputStream = getClass().getResourceAsStream("/relatorios/RelatorioCompra.jasper");
//        btnPRIMEIRO.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/primeiro.png"))); // NOI18N

        Map parametros = new HashMap();

        List dados = consultar("");

        // criando o datasource com os dados criados
        JRDataSource ds = new JRBeanCollectionDataSource(dados);

        try {
            // passando o datasource para o método de criação e exibição do relatório
            ReportUtils.openReport("Compras - Bean Collection Data Source", inputStream, parametros, ds);

        } catch (Exception ex) {
            retorno =  ex;
        }
        return retorno;
    }
}

Customer DAO

package dao;

import java.util.ArrayList;
import model.ClienteModel;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

public class ClienteDao {

    public Exception incluir(ClienteModel obj) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction t = session.beginTransaction();
            session.save(obj);
            t.commit();
            session.close();
            return null;
        } catch (Exception ex) {
            return ex;
        }
    }

    public Exception alterar(ClienteModel obj) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction t = session.beginTransaction();
            session.update(obj);
            t.commit();
            session.close();
            return null;
        } catch (Exception ex) {
            return ex;
        }
    }

    public ArrayList<ClienteModel> consultar(String filtro) {
        String sql = "from " + ClienteModel.class.getName() + filtro;

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        ArrayList lista = (ArrayList<ClienteModel>) session.createQuery(sql).list();
        t.commit();
        session.close();
        return lista;
    }

    public Exception excluir(ClienteModel pessoa) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction t = session.beginTransaction();
            session.delete(pessoa);
            t.commit();
            session.close();
            return null;
        } catch (Exception ex) {
            return ex;
        }
    }

    public ClienteModel get(long id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (ClienteModel) session.load(ClienteModel.class, id);
    }
}

Pessoa DAO

package dao;

import java.io.Serializable;
import java.util.ArrayList;
import model.PessoaModel;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

public class PessoaDao implements Serializable {

    public Exception incluir(PessoaModel obj) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction t = session.beginTransaction();
            Serializable ser = session.save(obj);
            t.commit();
            session.close();
            return null;
        } catch (Exception ex) {
            return ex;
        }
    }

    public Exception alterar(PessoaModel obj) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction t = session.beginTransaction();
            session.update(obj);
            t.commit();
            session.close();
            return null;
        } catch (Exception ex) {
            return ex;
        }
    }

    public ArrayList<PessoaModel> consultar(String filtro) {
        String sql = "from " + PessoaModel.class.getName() + filtro;

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        ArrayList lista = (ArrayList<PessoaModel>) session.createQuery(sql).list();
        t.commit();
        session.close();
        return lista;
    }

    public Exception excluir(PessoaModel pessoa) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction t = session.beginTransaction();
            session.delete(pessoa);
            t.commit();
            session.close();
            return null;
        } catch (Exception ex) {
            return ex;
        }
    }

    public PessoaModel get(long id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (PessoaModel) session.load(PessoaModel.class, id);
    }
}

Excerpt that takes the data of the Person and the Client to persist

ClienteController clientecontroller = new ClienteController();
                    PessoaController pessoacontroller = new PessoaController();
                    PessoaModel pessoa = new PessoaModel();
                    ClienteModel cliente = new ClienteModel();
                    cliente.setCLI_LIMITECRED(Double.parseDouble(edtCLI_LIMITECRED.getText()));
                    pessoa.setPES_NOME(edtPES_NOME.getText());
                    pessoa.setPES_FANTASIA(edtPES_FANTASIA.getText());
                    pessoa.setPES_FISICA((cbPES_FISICA.isSelected() ? 'S' : 'N'));
                    pessoa.setPES_CPFCNPJ(edtPES_CPFCNPJ.getText());
                    pessoa.setPES_RGIE(edtPES_RGIE.getText());

                    pessoa.setPES_ENDERECO(edtPES_ENDERECO.getText());
                    pessoa.setPES_NUMERO(edtPES_NUMERO.getText());
                    pessoa.setPES_COMPLEMENTO(edtPES_COMPLEMENTO.getText());               
                    pessoa.setPES_BAIRRO(edtPES_BAIRRO.getText());
                    pessoa.setPES_CIDADE(edtPES_CIDADE.getText());
                    pessoa.setPES_UF(edtPES_UF.getText());
                    pessoa.setPES_CEP(edtPES_CEP.getText());
                    pessoa.setPES_FONE1(edtPES_FONE1.getText());
                    pessoa.setPES_FONE2(edtPES_FONE2.getText());
                    pessoa.setPES_CELULAR(edtPES_CELULAR.getText());
                    pessoa.setPES_SITE(edtPES_SITE.getText());
                    pessoa.setPES_EMAIL(edtPES_EMAIL.getText());
                    pessoa.setPES_ATIVO((cbPES_ATIVO.isSelected() ? 'S' : 'N'));               
                    //pessoacontroller.gravar(pessoa, "incluir");
                    cliente.setPessoamodel(pessoa);


                        clientecontroller.gravar(cliente, "incluir");
                        mensagem = "Dados Gravados com Sucesso";
                } catch (Exception ex) {
                    mensagem = "Erro na Gravação do Usuário \n" + ex.getMessage();
                }
  • you need to save person before saving client

  • I get it, but does Hibernate not do automatic? And with Factory Séssion does not do it for me? And how do you recommend me to do it?? My teacher said Hibernate does it alone

  • Precisely.. the Error clearly says that you are trying to save a "Transient" value in a nonzero relation. You can change the relationship between Customer->Personal Modelmodel to perform a Calendar at the moment of persisting the Customer Model (persisting the person at this time.. ). Or, you need to save the Personal Model and then save the Customer Model p/ the JPA to have a "Managed" entity of the Personal Model to be able to relate to the Customer Model (the value of the FK must exist, basically)

  • How do I do this Scade? can help me?

  • 1

    Try to change the mapping of the Customer relation->Personal model p/ to make Cascade. @OneToOne(/*... */, cascade = CascadeType.PERSIST)

  • @Israelmerljak I made this modification, but gave the same mistake that I did not register person before

  • @Israelmerljak I switched to LAZY @Onetoone(fetch = Fetchtype.LAZY, Scade = Cascadetype.ALL) And now he’s made another mistake

  • @Israelmerljak Take a look at Bug, I changed the question

  • haha, man.. then save the Personal Model first, then save the Customer Model. What mistake did you make now? (I have no experience with the Hibernate-specific API, I prefer to use the same JPA API..)

  • That error is because it gave conflict in the restrictions of the base.. during insertion he tried to put a non-existent value (at least at the time of the attempt) p/ the field that has the Constraint FK_CLIENTE_PESSOA..

  • @Israelmerljak fuck, I don’t know what to do kkkk

  • @Israelmerljak has some idea?

  • @Israelmerljak Look at Edit, it’s putting the right key, I managed to pull the sql data with Log4j

  • I really needed to fix this )): @Israelmerljak

  • Putz.. I didn’t have time to see face. I was on the D: It seems to me that it might be a problem at the base. It doesn’t make much sense if the inserts are in the correct order. It can be a poorly made Constraint.

  • @Israelmerljak ta certo, pes_codigo table client is related to pes_codigo table person

  • Dude I discovered the error @Israelmerljak I took the foreign key, and I realized that in the database is putting PES_CODIGO = 2 and CLI_CODIGO = 2, it seems that to do the Insert is running the sequence twice...

  • But Ibernate takes the sequence of the first Insert and puts, alas for the error.. how it helps me to proceed further?

  • Dude, a question. Was this database created with Hibernate or is it handmade? (I’ll see if I can reproduce your project here and see what happens)

  • I got! THE SEQUENCE TRIGGER IN THE BANK WAS GENERATING WRONG SEQUENCE ALONG WITH THE HIBERNATE, I DEACTIVATED THE SEQUENCE TRIGGER AND NOW I AM USING ONLY HIBERNATE FOR SEQUENCES!!!

Show 15 more comments
No answers

Browser other questions tagged

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