Hibernate version: 2.1.6 ( the same one integrated in Jboss AS 4.0.1sp1 )
Mapping documents:
Mapping use composite keys (id x beginDateTime x versionBegin )
( Note: versionBegin isn't hibernate version)
Name and version of the database you are using: Oracle 9i
Hi,
I have the following code:
Code:
Session hsession = HibernateContext.getSession("java:/hibernate/TestSessionFactory");
Transaction tx = hsession.beginTransaction();
try
{
for (int i = 0; i < ObjectArrayValues.size(); i++)
{
int id = ((ObjectBean) ObjectArrayValues.get(i)).getID();
Date beginDate = ((ObjectBean) vObjectArrayValues.get(i)).getBeginDateTime();
Query q = hsession
.createQuery("from ObjectBean as cmv where " +
"cmv.id = :Id and " +
"cmv.beginDateTime <= :beginDateTime and " +
"cmv.versionEnd is null");
q.setInteger("Id", id);
q.setTimestamp("beginDateTime", beginDate);
List lCmv = q.list();
ObjectBean oBean = null;
if (lCmv.size() != 0)
oBean = (ObjectBean) lCmv.get(0);
if (oBean != null)
{
oBean.setVersionEnd(new Date());
hsession.update(oBean);
}
ObjectBean oValue = (ObjectBean) ObjectArrayValues.get(i);
oArrayValues.setVersionBegin(new Date());
oArrayValues.setVersionEnd(null);
hsession.save(oValue);
hsession.flush();
}
hsession.flush();
tx.commit();
}
catch (HibernateException e)
{
log.error("Error:", e);
tx.rollback();
}
when I do:
hsession.save(oValue);
hsession.flush();
hibernate produce:
1º iteration: 1 insert and 2 update with same values
2º iteration: 1 insert and 4 updates with same values
3º iteration: 1 insert and 6 updates with same values
why hibernate produce so many updates when I only want an Insert ??