Hibernate version: 2.1
Name and version of the database you are using:8.1-407 JDBC 3
I'm having a problem when I try to insert a entity (Loan) in my database using hibernate. This entity has a set (one-to-many) relationship with another entity (LoanParcel). The entity (LoanParcel), in turn, has a set (one-to-many) relationship with the entity (LoanParcelPayment). Below it's a part of their respective HBM's files.
Code:
<class name="Loan">
<id name="id" unsaved-value="-1">
<generator class="increment"/>
</id>
....
<set name="parcelas" cascade="all" lazy="true">
<key column="id_loan"/>
<one-to-many class="LoanParcel"/>
</set>
<set name="historicosStatus" lazy="true" table="idLoan_idHistoricoStatus">
<key column="id_loan"/>
<many-to-many class="HistoricoStatus"
column="id_historicoStatus"/>
</set>
</class>
Code:
<class name="LoanParcel">
<id name="id" unsaved-value="-1">
<generator class="increment"/>
</id>
...
<set name="pagamentos" cascade="all">
<key column="id_loanparcel"/>
<one-to-many class="LoanParcelPayment"/>
</set>
</class>
Code:
<class name="LoanParcelPayment">
<id name="id" unsaved-value="-1">
<generator class="increment"/>
</id>
...
</class>
The problem is that when I insert a (Loan) entity with its dependencies (Loan->LoanParcel) and (LoanParcel->LoanParcelPayment). The dependencies (LoanParcel and LoanParcelPayment) are being updated automatically. So I'm having a big overhead to process this operation. I only want to insert the father entity and it's dependences. I need no updates more.
Any idea to this behavior ? My HBM files have some problem? Why Hibernate needs additional updates to set parent id? Why set to null before? There is some way to avoid it and set parent id using the insert itself?
Attached it's a generated SQL of the operation.
Code:
Hibernate: select parametriz0_.id as id, parametriz0_.contaUsada as contaUsada, parametriz0_.utilizarSolicitacao as utilizar3_, parametriz0_.validadeSolicitacao as validade4_, parametriz0_.tipoBoleto as tipoBoleto, parametriz0_.tempoCastigo as tempoCas6_, parametriz0_.formatoPagamento as formatoP7_, parametriz0_.empresa as empresa from Emprestimo.ParametrizacaoLoan parametriz0_ where (parametriz0_.empresa=? )
[INFO ][2006-07-28:04:21:24]= (com.empresa.infra.log.Logger.info(38)) - Fechando a sessão?
Hibernate: insert into Emprestimo.Loan (codigo, data, moeda, valorSolicitado, valorAprovado, finalidade, metodoCalculo, baseJuros, juros, jurosMora, capitalizacao, metodoPagamentoJuros, frequenciaPagamento, freqJurosAcumulados, freqLancamentoJuros, numeroParcelas, tipoMulta, multa, tipoTAC, TAC, IOF, CPMF, valorDOC, ISS, tarifaPostal, carencia, enviarParaCartorio, diaPagamento, dataInicioPagamento, formaPagamento, numeroCheque, antecipacaoPrincipal, valorAntecipacaoPrincipal, antecipacaoJuros, valorAntecipacaoJuros, status, finalizado, dataStatus, id_agencia, id_solicitacaoEmprestimo, id_cliente, id_produto, id_atividade, id_agenteCredito, id_contaCorrenteCliente, id_contaEmpresa, id_usuarioDoCadastro, id_usuarioDoStatus, padraoConfiguracao, idEmprestimoOrigem, id_nivelRisco, id_tipoGarantia, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Emprestimo.LoanParcel (tipo, numero, vencimento, principal, juros, valor, correcao, saldoDevedor, status, ultimaAntecipacao, id_nivelRisco, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Emprestimo.LoanParcelPayment (numero, dataPagamento, principal, juros, valor, descontoJuros, descontoPrincipal, descontoMora, descontoMulta, desconto, outrasDeducoes, mora, multa, outrosAcrescimos, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Emprestimo.LoanParcel (tipo, numero, vencimento, principal, juros, valor, correcao, saldoDevedor, status, ultimaAntecipacao, id_nivelRisco, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Emprestimo.LoanParcelPayment (numero, dataPagamento, principal, juros, valor, descontoJuros, descontoPrincipal, descontoMora, descontoMulta, desconto, outrasDeducoes, mora, multa, outrosAcrescimos, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Common.HistoricoStatus (data, status, motivo, id) values (?, ?, ?, ?)
Hibernate: update Emprestimo.LoanParcel set id_loan=null where id_loan=?
Hibernate: update Emprestimo.LoanParcelPayment set id_loanparcel=null where id_loanparcel=?
Hibernate: update Emprestimo.LoanParcel set id_loan=null where id_loan=?
Hibernate: update Emprestimo.LoanParcelPayment set id_loanparcel=null where id_loanparcel=?
Hibernate: update Emprestimo.LoanParcel set id_loan=? where id=?
Hibernate: update Emprestimo.LoanParcelPayment set id_loanparcel=? where id=?
Hibernate: update Emprestimo.LoanParcel set id_loan=? where id=?
Hibernate: update Emprestimo.LoanParcelPayment set id_loanparcel=? where id=?
Hibernate: insert into Emprestimo.idLoan_idHistoricoStatus (id_loan, id_historicoStatus) values (?, ?)
Hibernate: update Emprestimo.LoanParcelPayment set id_loanparcel=? where id=?
Hibernate: select parametriz0_.id as id, parametriz0_.contaUsada as contaUsada, parametriz0_.utilizarSolicitacao as utilizar3_, parametriz0_.validadeSolicitacao as validade4_, parametriz0_.tipoBoleto as tipoBoleto, parametriz0_.tempoCastigo as tempoCas6_, parametriz0_.formatoPagamento as formatoP7_, parametriz0_.empresa as empresa from Emprestimo.ParametrizacaoLoan parametriz0_ where (parametriz0_.empresa=? )
Hibernate: update Emprestimo.LoanApplication set codigo=?, dataSolicitacao=?, valorSolicitado=?, numeroParcelas=?, baseJuros=?, taxaJuros=?, frequenciaPagamento=?, diaPagamento=?, carencia=?, finalidadeEmprestimo=?, motivoEmprestimo=?, status=?, dataStatusAtual=?, complementoStatus=?, validade=?, id_agencia=?, id_cliente=?, id_produto=?, id_atividade=?, id_agenteCredito=?, parecerAgenteCreditoFavoravel=?, complementoParecerAgenteCredito=?, id_tipoGarantia=?, id_usuarioDoCadastro=?, id_usuarioDoStatus=? where id=?
Hibernate: update Common.HistoricoStatus set data=?, status=?, motivo=? where id=?
Hibernate: update Garantia.Collateral set id_loanApplication=null where id_loanApplication=?
Hibernate: delete from Emprestimo.idLoanApplication_idFiador where id_loanApplication=?
Hibernate: update Emprestimo.LoanParcel set id_loan=null where id_loan=?
Hibernate: update Emprestimo.LoanParcelPayment set id_loanparcel=null where id_loanparcel=?
Hibernate: update Emprestimo.LoanParcel set id_loan=null where id_loan=?
Hibernate: update Emprestimo.LoanParcelPayment set id_loanparcel=null where id_loanparcel=?
Hibernate: update Emprestimo.LoanParcel set id_loan=? where id=?
Hibernate: update Emprestimo.LoanParcelPayment set id_loanparcel=? where id=?
Hibernate: update Emprestimo.LoanParcel set id_loan=? where id=?
Hibernate: update Emprestimo.LoanParcelPayment set id_loanparcel=? where id=?
Kind Regards,