Problem Explanation:
I am setting up a table to do insert using Oracle Sequence.The index field (<id> field) is getting different values than what is retreived by sequence.nextval DB call. I am using Oracle 10g, Hibernate (3.0.5). The column is question is the primary key, a NUMBER(10). The underlying property is a Long, set to null (as it is a new object). The generator class is set to a sequence, pointing to a value Oracle sequence. No errors are being generated when I do the .save.
For example, when I debug the following snippet, I get a ID value = 10 from DB (the Hibernate log shows call made to DB sequence obj). The value gets assigned to the batchItemId property in the value object (PaymentBatchItem) below.
But when Hibernate commits this transaction. the actual value inserted for the corresponding record has ID = 11. It skips the value retrieved by sequence object call for the same Insert..!
It almost seems that Hibernate makes a 1st call to sequence.nextVal, it increments the sequence by 1 then again when it calls a Insert statement, it does not take into account the previously retreived sequence #.
I have not read about this issue on the forum before. So, I think I might be missing something.
I tried changing my ID columns datatype to Integer instead of Long, tried to flush the session but I am getting the same problem.
Any help is appreciated.
thanks.
Hibernate version: 3.0.5
Mapping documents: PaymentBatchItem.hbm.xml
<class name="com.PaymentBatchItem" table="payment_batch_item">
<id name="batchItemId" type="long" column="batch_item_id">
<generator class="sequence">
<param name="sequence">seq_batch_item_id</param>
</generator>
</id>
<property name="loan">
<column name="loan" sql-type="varchar" length="15" not-null="true"/>
</property>
<property name="batchDate">
<column name="batch_dt" sql-type="datetime" not-null="true"/>
</property>
<property name="batchItemNumber">
<column name="batch_item_nbr" sql-type="integer" not-null="true"/>
</property>
<property name="draftId" type="long">
<column name="draft_id" not-null="false"/>
</property>
<property name="traceNumber" type="long">
<column name="trace_nbr" not-null="false"/>
</property>
</class>
Code between sessionFactory.openSession() and session.close():
public static List insert(List batchItems)
{
Session sess = null;
try
{
sess = HibernateUtil.getSession(HibernateUtil.DATASOURCE_EPS);
HibernateUtil.beginTransaction(HibernateUtil.DATASOURCE_EPS);
Iterator it = batchItems.iterator();
while(it.hasNext())
{
PaymentBatchItem pbi = (PaymentBatchItem) it.next();
//Long id = (Long) sess.save(pbi);
sess.saveOrUpdate(pbi);
}
HibernateUtil.commitTransaction(HibernateUtil.DATASOURCE_EPS);
} catch(HibernateException he)
{
HibernateUtil.rollbackTransaction(HibernateUtil.DATASOURCE_EPS);
he.printStackTrace(System.err);
} finally
{
HibernateUtil.closeSession(HibernateUtil.DATASOURCE_EPS);
}
return batchItems;
}
Name and version of the database you are using: Oracle 10g
The generated SQL (show_sql=true):
Hibernate: select seq_batch_item_id.nextval from dual
Hibernate: select seq_batch_item_id.nextval from dual
Hibernate: select seq_batch_item_id.nextval from dual
EPS transaction present.
Hibernate: insert into payment_batch_item (loan, batch_dt, batch_item_nbr, draft_id, trace_nbr, batch_item_id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into payment_batch_item (loan, batch_dt, batch_item_nbr, draft_id, trace_nbr, batch_item_id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into payment_batch_item (loan, batch_dt, batch_item_nbr, draft_id, trace_nbr, batch_item_id) values (?, ?, ?, ?, ?, ?)
|