-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Insert followed by extra update (one-to-many class mapping)
PostPosted: Mon May 15, 2006 3:56 pm 
Newbie

Joined: Mon May 15, 2006 1:52 pm
Posts: 2
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.java
Code:
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=?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 15, 2006 4:55 pm 
Newbie

Joined: Wed Dec 07, 2005 1:27 pm
Posts: 8
It's because of inverse="false". See section 1.3.6 of the Hibernate Reference.


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Tue May 16, 2006 2:35 pm 
Newbie

Joined: Mon May 15, 2006 1:52 pm
Posts: 2
Thanks for your time kleuthold!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.