Hi, thanks for the reply..
Here is my mapping file and the actual code..
---------------------------------------------------------------------
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>
<!--
Created by the Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="au.com.hybrid.warehouse.tables.ServerFieldLastUpdate"
table="server_field_last_update"
>
<id
name="fieldUpdateId"
type="java.lang.Long"
column="field_update_id"
>
<generator class="hilo">
<param name="table">server_field_last_update_key</param>
<param name="column">next_value</param>
<param name="max_lo">0</param>
</generator>
</id>
<property
name="tableName"
type="java.lang.String"
column="table_name"
not-null="true"
length="255"
/>
<property
name="itemId"
type="java.lang.String"
column="item_id"
not-null="true"
length="50"
/>
<property
name="fieldName"
type="java.lang.String"
column="field_name"
not-null="true"
length="100"
/>
<property
name="timestamp"
type="java.sql.Timestamp"
column="timestamp"
length="19"
/>
<property
name="deleted"
type="java.lang.String"
column="deleted"
not-null="true"
length="2"
/>
<property
name="inserted"
type="java.lang.String"
column="inserted"
not-null="true"
length="2"
/>
<!-- associations -->
</class>
</hibernate-mapping>
----------------------------------------------------------------------
CODE 1
Code:
for (int j=0;j<fieldUpdates.size();j++){
...
...
..
(**) Session sess = WarehouseSessionFactory.getSession(); // this method get the session
(**) Transaction txFieldUpdate = sess.beginTransaction();
for (int i=0; i<methods.size(); i++) {
ServerFieldLastUpdate sfluBean = new ServerFieldLastUpdate();
sfluBean.setTableName(tableName);
sfluBean.setItemId(itemIdString);
sfluBean.setInserted("Y");
sfluBean.setTimestamp(new Date((new java.util.Date()).getTime()));
sfluBean.setFieldName(methodName);
sfluBean.setDeleted("N");
sess.saveOrUpdate(sfluBean);
}
(**) txFieldUpdate.commit();
(**) sess.close();
}
----------------------------------------------------------------------
CODE 2
Code:
Session sess = WarehouseSessionFactory.getSession(); // this method get the session
Transaction txFieldUpdate = sess.beginTransaction();
for (int j=0;j<fieldUpdates.size();j++){
...
...
..
for (int i=0; i<methods.size(); i++) {
ServerFieldLastUpdate sfluBean = new ServerFieldLastUpdate();
sfluBean.setTableName(tableName);
sfluBean.setItemId(itemIdString);
sfluBean.setInserted("Y");
sfluBean.setTimestamp(new Date((new java.util.Date()).getTime()));
sfluBean.setFieldName(methodName);
sfluBean.setDeleted("N");
sess.saveOrUpdate(sfluBean);
}
}
txFieldUpdate.commit();
sess.close();
------------------------------------------------------------------------
A sidenote, that the two code are
exactly the same, apart from that I move the
line marked by (**) in CODE 1 out of the loop. The reason being (as I had mentioned):
- performance (one session, one transaction, vs 1000 session, 1000 transaction)
- ability to rollback the whole insertion (it is possible to do in code 2, but it has
to be done manually, i.e. recording what has been inserted, and delete them manually)
and just to reply to some of your questions,
1. I'm using hilo algorithm for the primary key generation, and no, it's not a composite id at all
(and i don't think i need composite id for this, since CODE 1 works fine).
2. ".....could be because an object with the same id key(s)/value(s) is in the session"
Umm.. i don't think this is the problem here, since as you can see, the two codes are the same,
and I call "new ServerFieldLastUpdate()" each time, and setting different values. Also the fact
that code 1 works, i don't think this is the reason why code 2 does not work. Even if that
was the reason, at least 1 object should get saved (and the following did not, since Hibernate
has detected that an object with the same id / value is in the session), but none of the object
get saved.
There wouldn't be a maximum size of objects a Session can store before being persisted, would it?
(I hope so.... )
Thanks in advance for the help!
Regards,
Alex.