I have two classes:
===============================================
ContaLancamento.java
===============================================
public class ContaLancamento {
...
/**
* @hibernate.set
* inverse="true"
* lazy="true"
* cascade="save-update"
* @hibernate.collection-key
* column="ID_CONTA_LANCAMENTO"
* @hibernate.collection-one-to-many
* class="com.expert.pojos.ContaLancamentoParcela"
*/
public Set getContasLancamentosParcelas() {
return this.contasLancamentosParcelas;
}
public void addContaLancamentoParcela(ContaLancamentoParcela
contaLancamentoParcela) {
contaLancamentoParcela.setContaLancamento(this);
this.contasLancamentosParcelas.add(contaLancamentoParcela);
}
}
===============================================
and
===============================================
ContaLancamentoParcela.java
===============================================
public class ContaLancamentoParcela {
...
/**
* @hibernate.many-to-one
* column="ID_CONTA_LANCAMENTO"
* cascade="save-update"
* not-null="true"
* @return
*/
public ContaLancamento getContaLancamento() {
return this.contaLancamento;
}
public void setContaLancamento (ContaLancamento contaLancamento) {
this.contaLancamento = contaLancamento;
}
}
===============================================
If you read the code above, you will see ContaLancamento is a class "father" and ContaLancamentoParcela is a "child" class.
When I create a new ContaLancamento object, I try to insert it in the data base, using Hibernate (note: I'm inserting inside ContaLancamento new ContaLancamentoParcela objects)
===============================================
CÓDE 1
===============================================
ContaLancamento contaLancamento = (ContaLancamento)getEditableObject();
SContasLancamentos service = new SContasLancamentos(usuario);
service.insert(contaLancamento);
Iterator itr = tableParcelas.iterator();
while (itr.hasNext()) {
ContaLancamentoParcela contaLancamentoParcela =
(ContaLancamentoParcela)itr.next();
contaLancamento.addContaLancamentoParcela(contaLancamentoParcela);
service.insertParcela(contaLancamentoParcela);
}
===============================================
Well, the code above works fine, and it is wonderful. But it's not make sense to me.
Look it. If ContaLancamento is "father" of a lot of objects ContaLancamentoParcela, why should I have to insert "contaLancamentoParcela" with my hands in the code above?
So, I think..., I should be only save the object ContaLancamento and Hibernante will save the children too. The property "cascade" was set to "save-update" and I think Hibernate should be save the children automatically.
So, I wrote something like that:
===============================================
CÓDE 2
===============================================
ContaLancamento contaLancamento = (ContaLancamento)getEditableObject();
SContasLancamentos service = new SContasLancamentos(usuario);
Iterator itr = tabelaParcelas.iterator();
while (itr.hasNext()) {
ContaLancamentoParcela contaLancamentoParcela =
(ContaLancamentoParcela)itr.next();
contaLancamento.addContaLancamentoParcela(contaLancamentoParcela);
}
service.insert(contaLancamento);
===============================================
But, the code above didn't work. An exception was generate, like:
===============================================
ERROR AWT-EventQueue-0
org.hibernate.event.def.AbstractFlushingEventListener - Could not
synchronize database state with session
org.hibernate.StaleStateException: Batch update returned unexpected
row count from update: 0 actual row count: 0 expected: 1
===============================================
Questions:
A) If property "cascade" in "ContaLancamento" object is set to "save-update", why didn't code 2 work fine?
B) Should I have to do aways like code 1 e I want, using Hibernate, aways inserting a new instance of an object "father", with many children inside it ?
Thanks
Cristiano M. Magalhães
|