Hibernate version: 2.1.7c
Hi,
I'm curious about this particular issue. I have a small import program which does a batch insert of more or less 1500 records. I gave a look at the FAQ and I saw some hints about batch imports with Hibernate and used them.
Now the issue. The code looks like (a bit simplified, because I don't have the actual source here on my home PC):
Code:
for (int i = 0; i < rows.size(); i++) {
String row = (String) rows.get(i);
Transaction tx = session.beginTransaction();
try {
/* ...here I parse my columns from a text file row... */
Soggetto subject = new Soggetto(); // my hibernate POJO
subject.setOwner(owner);
/* ... I do all my sets with the strings I parsed... */
session.saveOrUpdate(owner); // kindly ask the session to save..
tx.commit();
ok++;
if (i % 100 == 0) session.flush(); // let's flush every 100 rows
}
catch (Exception ex) {
tx.rollback();
errors++;
}
}
The mapping file is :
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="it.blablabla.Soggetto" table="SOGGETTO">
<id name="id" column="ID" type="long">
<generator class="sequence" />
</id>
<!-- Business Key -->
<property name="committente" type="string">
<column name="COMMITTENTE" length="5" unique-key="bk" not-null="true" />
</property>
<property name="codice" type="string">
<column name="CODICE" length="20" unique-key="bk" not-null="true" />
</property>
<property name="tipoSoggetto" column="TIPO_SOGGETTO" type="string" length="5" />
<property name="ragioneSociale" column="RAGIONE_SOCIALE" type="string" length="50" />
<property name="ragioneSociale2" column="RAGIONE_SOCIALE2" type="string" length="50" />
<property name="indirizzo" column="INDIRIZZO" type="string" length="50" />
<property name="localita" column="LOCALITA" type="string" length="50" />
<property name="cap" column="CAP" type="string" length="10" />
<property name="provincia" column="PROVINCIA" type="string" length="50" />
<property name="nazione" column="NAZIONE" type="string" length="50" />
<property name="ragioneSocialeDest" column="RAGIONE_SOCIALE_D" type="string" length="50" />
<property name="ragioneSociale2Dest" column="RAGIONE_SOCIALE2_D" type="string" length="50" />
<property name="indirizzoDest" column="INDIRIZZO_D" type="string" length="50" />
<property name="localitaDest" column="LOCALITA_D" type="string" length="50" />
<property name="capDest" column="CAP_D" type="string" length="10" />
<property name="provinciaDest" column="PROVINCIA_D" type="string" length="50" />
<property name="nazioneDest" column="NAZIONE_D" type="string" length="50" />
<property name="vettoreDefault" column="VETTORE_DEFAULT" type="string" length="20" />
<property name="flagVettore" column="FLAG_VETTORE" type="string" length="1" />
</class>
</hibernate-mapping>
After 1200 records there is a duplicated key, which is correctly caused by wrong legacy data in the file. The exception is trapped, the transaction rolled back, and everything's fine. But from record 1201, every INSERT throws a duplicated key exception, even neither the key nor my unique columns are duplicated! The Subject class has a generated sequence key, and it seems like that after the first error Hibernate tries to insert always the same ID, without fetching a new one.
I probably missed something. Adding session.clear() in the catch clause solves the problem, flushing every row doesn't. The mapping shoudl be correct, since all operations like INSERT / SAVE / UPDATE via Hibernate work fine.
Where am I wrong ?
Thanks for any suggestion.
Giulio