-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem with saveOrUpdate
PostPosted: Mon Nov 13, 2006 3:29 pm 
Newbie

Joined: Tue Sep 19, 2006 7:07 pm
Posts: 9
I'm trying to insert in one table with saveOrUpdate. I created a new object and I'm using the assigned generator. Hibernate is executing update and not insert. What is wrong? The code is the follow:

HBM file:

<class name="dto.Endereco"
table="endereco">

<id name="codigoPess" column="CODIGO_PESS" type="java.lang.Integer" unsaved-value="0" >
<generator class="assigned">
</generator>
</id>

<property
name="rua"
type="java.lang.String"
column="RUA"
/>
...
</class>

java file:

public class Endereco {

private int codigoPess;
private String rua;
...
getters and setters...
}

DAO java file:

public void saveOrUpdate( Session session, List<Endereco> enderecos )
Transaction transaction = session.beginTransaction();
for (Iterator iterator = enderecos.iterator(); iterator.hasNext();) {
Endereco endereco = (Endereco) iterator.next();
session.saveOrUpdate(endereco);
}
transaction.commit();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
Session session = HibernateUtil.currentSession();
List<Endereco> list = new ArrayList();
Endereco endereco1 = new Endereco();
endereco1.setCodigoPess(3);
endereco1.setRua("xx");
...
list.add(endereco);
EnderecoDAO enderecoDAO = new EnderecoDAO();
enderecoDAO.saveOrUpdate( session, list );
HibernateUtil.closeSession();
}

console output:

Hibernate: update endereco set RUA=?, NUMERO=?, BAIRRO=?, CIDADE=?, ESTADO=?, CEP=?, CODIGO_AREA=? where CODIGO_PESS=?

If I change the generator to increment and hide the line endereco1.setCodigoPess(3); it works.

console output:
Hibernate: select max(CODIGO_PESS) from endereco
Hibernate: insert into endereco (RUA, NUMERO, BAIRRO, CIDADE, ESTADO, CEP, CODIGO_AREA, CODIGO_PESS) values (?, ?, ?, ?, ?, ?, ?, ?)

I need to set CodigoPess by myself and not generate automaticaly.

How can I fix the problem?

Ps> Sorry for my bad english...


Top
 Profile  
 
 Post subject: Can you ...
PostPosted: Tue Nov 14, 2006 12:32 am 
Newbie

Joined: Mon Jun 20, 2005 11:38 am
Posts: 7
Post your class code.

but Choosing the assigned generator makes Hibernate use unsaved-value="undefined" (you try to use 0) so try to remove unsaved-value="0"


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.