I’m testing an insert of one parent record and 10,000 child records into a mySQL (v. 4.1.9-standard) database using a one-to-many join via Hibernate 2.1 and am getting transaction times in excess of 20 minutes! I see that Hibernate...
(1) inserts the parent record,
(2) inserts the child records without the parent id, and then
(3) updates each child record with the parent id.
This third step is approximately 98% of the total transaction time. How can I eliminate this update step and have the parent id included in the original insert of the child records (step #2)? Any help would be enormously appreciated!
-- Michael Ceci
Hibernate version: 2.1
Name and version of the database you are using: mySQL 4.1.9-standard
parent.hbm.xml:
Code:
<hibernate-mapping>
<class name="com.test.Parent" table="parent_table">
<id name="id" column="parent_id" type="long" unsaved-value="null">
<generator class="increment"/>
</id>
<property name="name" column="parent_name"/>
<set name="children" cascade="all" inverse="false">
<key column="parent_id"/>
<one-to-many class="com.test.Child"/>
</set>
</class>
</hibernate-mapping>
child.hbm.xml:Code:
<hibernate-mapping>
<class name="com.test.Child" table="child_table">
<id name="id" column="child_id" type="long" unsaved-value="null">
<generator class="increment"/>
</id>
<property name="name" column="child_name" type="string"/>
</class>
</hibernate-mapping>
Java Code:Code:
Set childrenSet = new HashSet();
Child child = null;
for(int i=0; i < 10000; i++) {
child = new Child("Child Name" + Integer.toString(i+1));
childrenSet.add(child);
}
Parent parent = new Parent("Parent Name", childrenSet);
long startTime = System.currentTimeMillis();
try {
log.debug(" >>> You're in AlertServiceImpl.insert()!! <<<");
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(alert);
tx.commit();
session.close();
} catch (HibernateException e) {
System.out.println(e.toString());
throw e;
}
long stopTime = System.currentTimeMillis();
System.out.println("TRANSACTION TIME = " + (stopTime - startTime) + " milliseconds);