Hi,
I think in the sequence of interaction between hibernate and db, id generation is not an issue. There are no hits for id generation. The problem is in the sequence of execution. Lets take a very simple example.
DeliveryOrder extends Order
DeliveryOrder has two attributes DeliveryAddress and BillingAddress.
So in the mapping -
Code:
<class name="Order" table="AbsOrder" >
<id name="id" column="id" type="long" />
<property name="amt" />
<property name="orderCurrency" column="Currency"/>
<property name="customerId" column="customer_Id" />
</class>
<joined-subclass name="DeliveryOrder" extends="Order" table="DeliveryOrder" select-before-update="false" >
<key> <column name="id" /> </key>
<property name="deliveryDate" column="Delivery_Date" />
<property name="deliveryNotes" column="Note" />
<many-to-one name="deliveryAddress" class="Address" column="del_addr_id" cascade="save-update" />
<many-to-one name="billingAddress" class="Address" column="bill_addr_id" cascade="save-update" />
</joined-subclass>
This generates a following sequence of SQLs -
Code:
Hibernate: select max(id) from ADDRESS
Hibernate: insert into ADDRESS (city, country, zip, id) values (?, ?, ?, ?)
Hibernate: insert into ADDRESS (city, country, zip, id) values (?, ?, ?, ?)
---
---
Hibernate: insert into ADDRESS (city, country, zip, id) values (?, ?, ?, ?)
Hibernate: insert into AbsOrder (amt, Currency, customer_Id, id) values (?, ?, ?, ?)
Hibernate: insert into DeliveryOrder (Delivery_Date, Note, del_addr_id, bill_addr_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into AbsOrder (amt, Currency, customer_Id, id) values (?, ?, ?, ?)
Hibernate: insert into DeliveryOrder (Delivery_Date, Note, del_addr_id, bill_addr_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into AbsOrder (amt, Currency, customer_Id, id) values (?, ?, ?, ?)
Hibernate: insert into DeliveryOrder (Delivery_Date, Note, del_addr_id, bill_addr_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into AbsOrder (amt, Currency, customer_Id, id) values (?, ?, ?, ?)
Hibernate: insert into DeliveryOrder (Delivery_Date, Note, del_addr_id, bill_addr_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into AbsOrder (amt, Currency, customer_Id, id) values (?, ?, ?, ?)
Hibernate: insert into DeliveryOrder (Delivery_Date, Note, del_addr_id, bill_addr_id, id) values (?, ?, ?, ?, ?)
Saving the batch no 1 in 1159
Address inserts have happened earlier, which helped in JDBC batching. However, DeliveryOrder and AbsOrder inserts have got interleaved causing batching to fail in AbstractBatcher. this can be clearly seen from the debug log
Code:
18:53:07,028 [main] DEBUG hibernate.jdbc.AbstractBatcher - reusing prepared statement
18:53:07,028 [main] DEBUG org.hibernate.SQL - insert into ADDRESS (city, country, zip, id) values (?, ?, ?, ?)
18:53:07,028 [main] DEBUG persister.entity.AbstractEntityPersister - Calling batcher from entity
18:53:07,028 [main] DEBUG hibernate.jdbc.BatchingBatcher - called addToBatch
-----
18:53:07,043 [main] DEBUG hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
18:53:07,043 [main] DEBUG org.hibernate.SQL - insert into AbsOrder (amt, Currency, customer_Id, id) values (?, ?, ?, ?)
18:53:07,043 [main] DEBUG persister.entity.AbstractEntityPersister - Calling batcher from entity
18:53:07,043 [main] DEBUG hibernate.jdbc.BatchingBatcher - called addToBatch
18:53:07,043 [main] DEBUG persister.entity.AbstractEntityPersister - Expectation allows batching : true: j 1
18:53:07,043 [main] DEBUG hibernate.jdbc.BatchingBatcher - Executing batch size: 1
18:53:07,059 [main] DEBUG hibernate.jdbc.Expectations - success of batch update unknown: 0
18:53:07,059 [main] DEBUG hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
18:53:07,059 [main] DEBUG hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
18:53:07,059 [main] DEBUG org.hibernate.SQL - insert into DeliveryOrder (Delivery_Date, Note, del_addr_id, bill_addr_id, id) values (?, ?, ?, ?, ?)
18:53:07,059 [main] DEBUG persister.entity.AbstractEntityPersister - Calling batcher from entity
18:53:07,059 [main] DEBUG hibernate.jdbc.BatchingBatcher - called addToBatch
18:53:07,059 [main] DEBUG persister.entity.AbstractEntityPersister - Expectation allows batching : true: j 0
18:53:07,059 [main] DEBUG hibernate.jdbc.BatchingBatcher - Executing batch size: 1
18:53:07,075 [main] DEBUG hibernate.jdbc.Expectations - success of batch update unknown: 0
18:53:07,075 [main] DEBUG hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
As log statements indicate, batching could be done for address but not for AbsOrder and DeliveryOrder. if the prepared statements were tracked by the AbstractBatcher, it would have been able to batch it correctly.