Hello,
I'm seeing Hibernate run a select statement for every object I try to persist with saveOrUpdate. How do I get rid of them?
I read in Java Persistence with Hibernate (pages 528-529) that this can happen when the objects have a natural composite key and no versioning, which is what our objects look like. So I added the versioning as in the mapping below and the select statements still happen. Is there still something else that causes the selects? Joined-subclass maybe?
Thanks!
Hibernate version:
3.2.5
Mapping documents:
<hibernate-mapping>
<class name="ModelElement"
table="MODEL_ELEMENTS">
<composite-id name="id" class="Identifier">
<key-property name="elementId" type="integer" column="ELEMENT_ID"/>
<key-property name="modelId" type="long" column="MODEL_ID"/>
</composite-id>
<version name="version" access="field" column="VERSION"/>
<properties here>
</class>
</hibernate-mapping>
<hibernate-mapping>
<joined-subclass
name="GlobalInfoElement"
extends="ModelElement"
table="GLOBAL_INFO_ELEMENTS">
<key>
<column name="GLOBAL_INFO_ELEMENT_ID"/>
<column name="GLOBAL_INFO_ELEMENT_MID"/>
</key>
<properties here>
</joined-subclass>
</hibernate-mapping>
<hibernate-mapping>
<joined-subclass
name="SchedulingGlobalInfoElement"
extends="GlobalInfoElement"
table="SCHEDULING_GLOBALS">
<key>
<column name="SCHEDULING_GLOBAL_ID"/>
<column name="SCHEDULING_GLOBAL_MID"/>
</key>
<properties here>
</joined-subclass>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Code:
try {
int counter = 0;
ModelElement[] mes = model.getOrderedElements();
for(ModelElement me : mes) {
session.saveOrUpdate(me);
counter++;
if (counter % HibernateUtil.BATCH_SIZE == 0 ) {
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
tx.commit();
}
catch (Exception e) {
DatabaseHandler.recordAndSendDatabaseError(e, true);
try {
tx.rollback();
} catch(HibernateException he) {
InfoLogger.error("Error: Unable to rollback transaction.");
}
}
Name and version of the database you are using:
Oracle 10.2
The generated SQL (show_sql=true):
Hibernate:
/* get current state SchedulingGlobalInfoElement */ select
scheduling_.SCHEDULING_GLOBAL_ID,
scheduling_.SCHEDULING_GLOBAL_MID,
scheduling_2_.ELEMENT_TYPE as ELEMENT3_0_,
scheduling_2_.IS_VISIBLE as IS4_0_,
scheduling_2_.NAME as NAME0_,
scheduling_.END_OF_TIME as END3_30_,
scheduling_.START_OF_TIME as START4_30_,
scheduling_.RESOURCE_SET_SUFFIX as RESOURCE5_30_,
scheduling_.ROOT_ACTIVITY_ID as ROOT6_30_,
scheduling_.ROOT_CALENDAR_ID as ROOT7_30_,
scheduling_.ROOT_FLOW_ID as ROOT8_30_,
scheduling_.ROOT_RESOURCE_ID as ROOT9_30_,
scheduling_.ROOT_RESOURCE_SET_ID as ROOT10_30_
from
SCHEDULING_GLOBALS scheduling_
inner join
GLOBAL_INFO_ELEMENTS scheduling_1_
on scheduling_.SCHEDULING_GLOBAL_ID=scheduling_1_.GLOBAL_INFO_ELEMENT_ID
and scheduling_.SCHEDULING_GLOBAL_MID=scheduling_1_.GLOBAL_INFO_ELEMENT_MID
inner join
MODEL_ELEMENTS scheduling_2_
on scheduling_.SCHEDULING_GLOBAL_ID=scheduling_2_.ELEMENT_ID
and scheduling_.SCHEDULING_GLOBAL_MID=scheduling_2_.MODEL_ID
where
scheduling_.SCHEDULING_GLOBAL_ID=?
and scheduling_.SCHEDULING_GLOBAL_MID=?