-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate does additional updates when inserting on cascade.
PostPosted: Fri Jul 28, 2006 4:02 pm 
Regular
Regular

Joined: Thu Jul 08, 2004 1:21 pm
Posts: 68
Location: Recife - Pernambuco - Brazil
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,

_________________
Marcos Silva Pereira
http://blastemica.blogspot.com


Top
 Profile  
 
 Post subject: cascade behavior
PostPosted: Fri Jul 28, 2006 5:08 pm 
Newbie

Joined: Fri Oct 14, 2005 4:42 pm
Posts: 15
If you want no updates to child objects, set cascade="none" in the mapping for that class.

See http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#objectstate-transitive for complete details of cascade behavior in hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 31, 2006 12:40 pm 
Regular
Regular

Joined: Thu Jul 08, 2004 1:21 pm
Posts: 68
Location: Recife - Pernambuco - Brazil
Thanks, I know that. I know how cascades works. My doubt is: why hibernate does additional updates to setting parent foreign key to "null" and later to set the correct id? Why not to set id using insert query?
Code:
Hibernate insert into Emprestimo.LoanParcel ...
Hibernate: update Emprestimo.LoanParcel set id_loan=null where id_loan=?
Hibernate: update Emprestimo.LoanParcel set id_loan=? where id=?

why Hibernate executes these query (especially the first)?
Code:
Hibernate: update Emprestimo.LoanParcel set id_loan=null where id_loan=?
Hibernate: update Emprestimo.LoanParcel set id_loan=? where id=?

Why not set parent id using insert query? There is some configuration to avoid this behavior?

Kind Regards,

_________________
Marcos Silva Pereira
http://blastemica.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 5:21 am 
Newbie

Joined: Thu Aug 03, 2006 5:12 am
Posts: 2
May be try to set for this (id_loan) field props: insert="true" update="false"


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 8:04 am 
Newbie

Joined: Thu Aug 03, 2006 5:12 am
Posts: 2
Sorry, I read Your message carelessnes. You insert parent entity but don't update.
May be this can help You.
Make in LoanParcel ref to parent (many-to-one) (bi-directional ref.) and set up it (not ID) before insert (it's an object ref.) and LoanParcel.id_loan dont touch at all (insert="false" update="false")


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.