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...
|