-->
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: Changes after save() generates INSERT+UPDATE
PostPosted: Tue Dec 30, 2008 8:55 am 
Newbie

Joined: Tue Dec 30, 2008 8:25 am
Posts: 2
I have noticed that the INSERT statement that Hibernate sends to the database always contains the values that were held by the object when Session.save() was called. This means that if the object changes after Session.save(), then another UPDATE will be issued (instead of combining the changes into one INSERT)

Does anybody know whether it is possible to combine all changes into the one INSERT?
If not, any idea why not? (i.e. is there a good reason, or is just the case that nobody has really asked for it)


Below a simple Java class that reproduces this issue:
Code:
public class SaveThenUpdateTest {
   public final static Logger log = Logger.getLogger(SaveThenUpdateTest.class);
   
   public static void main(String[] args) {
      log.info("Configuring connection");
      Configuration config = new Configuration()
         .addClass(A.class)
         .setProperty("hibernate.connection.driver_class", "...")
         .setProperty("hibernate.connection.url", "...")
         .setProperty("hibernate.connection.username", "...")
         .setProperty("hibernate.connection.password", "...")
         ;
      SessionFactory f = config.buildSessionFactory();
      Session s = f.openSession();
      Transaction tx = s.beginTransaction();
      
      A a = new A();
      a.setValueA("valuea");
      s.save(a);
      a.setValueB("valueb");

      log.info("Before commit");
      tx.commit();
      f.close();
      log.info("Finished tests");
   }
   
}



and this is what we get in the log:
Code:
INFO SaveThenUpdateTest:16 - Configuring connection
DEBUG SQL:401              - select max(id) from A
INFO SaveThenUpdateTest:33 - Before commit
DEBUG SQL:401              - insert into A (valueA, valueB, id) values (?, ?, ?)
DEBUG SQL:401              - update A set valueA=?, valueB=? where id=?
INFO SaveThenUpdateTest:36 - Finished tests


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 6:46 am 
Newbie

Joined: Tue Dec 30, 2008 8:25 am
Posts: 2
Hi Everybody

I hope somebody may be able to help, or at least throw in some light into this issue.

My problem is very simple. Basically Hibernate fires off both an INSERT and an UPDATE when I create a new instances, which is inefficient when creating thousands of instances within a batch.

I am surprised that Hibernate is not "intelligent" enough to combine all changes into the one INSERT.

Has anybody experienced this issue before?
Is there a way of configuring Hibernate to achieve this?

Many thanks
Eduard


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 9:09 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
yes friend, hibernate was configured in that way. becaz after save that value in DB, if u gonna to change anything it will use update. it doesn't compound these statement into one statement.

**) code is on our hand. if u did well, we will get good output.

_________________
If u feel it will help you, don't forget to rate me....


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.