It looks like hibernate is using an insert followed by "extra updates?" when creating objects having a one-to-many class mapping. These "extra updates" seem to be un-needed overhead. Is there a way to avoid these updates?
(Please see the sample code below with Message mapped to one-to-many comments)
Hibernate version: 3.1.3 with all required libs for standalone jdk1.4
Hibernate configuration:Code:
<session-factory>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost/test</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="hello/Message.hbm.xml"/>
<mapping resource="hello/Comment.hbm.xml"/>
</session-factory>
Message.hbm.xml:Code:
<class name="hello.Message" table="MESSAGES">
<id
name="id"
column="MESSAGE_ID"
type="long">
<generator class="increment"/>
</id>
<property
name="text"
column="MESSAGE_TEXT">
</property>
<set name="comments"
table="COMMENTS"
sort="unsorted"
inverse="false"
cascade="all-delete-orphan">
<key column="MESSAGE_ID"/>
<one-to-many class="hello.Comment"/>
</set>
</class>
Comment.hbm.xml:Code:
<class name="hello.Comment" table="COMMENTS">
<id
name="id"
column="COMMENT_ID"
type="long">
<generator class="increment"/>
</id>
<property
name="text"
column="COMMENT_TEXT">
</property>
</class>
Code between sessionFactory.openSession() and session.close():Code:
// Add a new message containing 2 comments.
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Message m = new Message();
m.setText("Greetings Earthling!");
Set comments = new HashSet();
Comment c = new Comment("Alien 1 jibberish...");
comments.add(c);
c = new Comment("Alien 2 jibberish...");
comments.add(c);
m.setComments(comments);
session.save(m);
session.getTransaction().commit();
HibernateUtil.javaCode:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Full stack trace of any exception that occurs:
No exceptions
Name and version of the database you are using:
HSQLDB 1.8.0.4
The generated SQL (show_sql=true):
select max(MESSAGE_ID) from MESSAGES
select max(COMMENT_ID) from COMMENTS
insert into MESSAGES (MESSAGE_TEXT, MESSAGE_ID) values (?, ?)
insert into COMMENTS (COMMENT_TEXT, COMMENT_ID) values (?, ?)
insert into COMMENTS (COMMENT_TEXT, COMMENT_ID) values (?, ?)
update COMMENTS set MESSAGE_ID=? where COMMENT_ID=?
update COMMENTS set MESSAGE_ID=? where COMMENT_ID=?