-->
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.  [ 1 post ] 
Author Message
 Post subject: how to insert correctly with Hibernate?
PostPosted: Mon May 14, 2007 10:16 pm 
Newbie

Joined: Mon May 14, 2007 9:58 pm
Posts: 7
Location: Bahia / Brasil
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


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

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.