Hi,
We are using:
Hibernate: 3.0
MySQL: 5.0.27
JBoss: 4.0.5
Java: J2SDK 1.4.2_08
We are facing a Database performance issue while implementing bidirectional mapping between two classes (LogRecordCache and LogRecord with relation: A LogRecordCache can have a List of LogRecords). Their hibernate mappings are as shown below:
1. ------ Mapping for LogRecordCache -----
Code:
<class name="LogRecordCache" table="wsc_log" lazy="false">
<id name="logId" column="logid" type="long">
<generator class="assigned" />
</id>
...
<list name="logs" lazy="false" cascade="save-update">
<key property-ref="logId" column="logid" />
<index column="indexcolumn" type="int"/>
<one-to-many class="LogRecord"/>
</list>
...
</class>
2. ------ Mapping for LogRecord -----
Code:
<class name="LogRecord" table="wsc_logrecords">
<id name="logRecordId" column="logrecordid" type="string">
<generator class="uuid.hex"/>
</id>
...
<many-to-one lazy="false" name="logRecordCache" column="logid" property-ref="logId"/>
...
</class>
we have observed that while saving an instance of LogRecordCache additional update queries are issued. So, instead of issuing N+1 queries (1 query to insert LogRecordCache, and N queries to insert N LogRecord in it), 2N+1 queries (with N additional updates) are issued. Ofcourse, these additional updates are used for populating the index column for the indexed collection (LogRecordCache's indexcolumn) and the foreign key (LogRecord.logId in our case).
Note that the updates are fired even when we use a bag, just that they update the foreign key(logId in our case) only.
Please suggest some way so that updates are not fired or indicate that this is default Hibernate behavior and if possible also why (gavin ????).
Thanks,
Rupesh.