-->
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.  [ 2 posts ] 
Author Message
 Post subject: select before save
PostPosted: Tue Jun 14, 2011 6:49 am 
Beginner
Beginner

Joined: Thu Jan 06, 2011 6:19 am
Posts: 25
Hi.

I want to insert many entities into my database via Hibernate. I use batch insertion but it seems to be too slow for my use case. When I debug the hibernate SQL I can see that it generates a SELECT statement for each session.save(...) call I use. I don't understand why? Is this the normal behaviour?

My entity:

Code:
@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true, dynamicUpdate=true)
@Cache(include = "non-lazy", usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "com.mypackage.impl.MyEntityImpl")
@Table(name = "PRICING_Price")
public class MyEntityImpl implements MyEntity, Serializable {

   @Id
   @Column(name = "ID", nullable = false, columnDefinition = "char(36)")
   private String ID = UUID.randomUUID().toString();
   
   // ... some fields
   
   @Version
   private int version;
}


My insertion code:
Code:

void insert() {
    int batchSize = 50;
    Session session = ... // get Session
    Transaction t = session.beginTransaction();
    for (int i=0; i<3000000; i++) {
          addEntity(...);
          if (i%batchSize == 0) {
                flushInserts(session);
          }
     }

     transaction.commit();
}
void addEntity(...) {
    MyEntity entity = new MyEntity Impl();
    // set properties
    myEntityQueue.add(entity);
}

void flushInserts(Session session) {
    for(MyEntity entity : myEntityQueue) {
              session.save(entity); // <--- generate SELECT statement
    }
    myEntityQueue.clear();
    session.flush(); // <--- generates 50 INSERT statements
    session.clear();
}


What can I do to speed up the insertion?

Regards, jacquipre.


Top
 Profile  
 
 Post subject: Re: select before save
PostPosted: Tue Jun 14, 2011 9:13 am 
Beginner
Beginner

Joined: Thu Jan 06, 2011 6:19 am
Posts: 25
Ok, when using
Code:
session.persist()

instead of
Code:
session.save()

no SELECT statements where created and my import is faster.

Guess that has something to do with optimistic locking?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.