2

This is my code to insert batch in database

  @Override
  public void addMultiple(){

    session = get_session();
  tx = session.beginTransaction();
 for ( int i=0; i<100; i++ ) {
 Person person = new Person();
 person.setName("oll"+i);
 session.save(person);
 if( i % 20 == 0 ) { // Same as the JDBC batch size
    //flush a batch of inserts and release memory:
    session.flush();
    session.clear();
   }
}
tx.commit();
session.close();
}

this is my hibernate.cfg.xml file

 <property  name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.jdbc.batch_versioned_data">true</property>
<property name="hibernate.jdbc.batch_size">20</property>
<property name="hibernate.order_inserts">true</property>
<property name="hibernate.current_session_context_class">thread</property>

but after insert operation i can see query is printed in console is

  Hibernate: insert into Person (name) values (?)
  Hibernate: insert into Person (name) values (?)
  Hibernate: insert into Person (name) values (?)
  Hibernate: insert into Person (name) values (?)
  Hibernate: insert into Person (name) values (?)
  Hibernate: insert into Person (name) values (?)
  Hibernate: insert into Person (name) values (?)
  Hibernate: insert into Person (name) values (?)
  Hibernate: insert into Person (name) values (?)
  Hibernate: insert into Person (name) values (?)
  Hibernate: insert into Person (name) values (?)

That means it doesn't insert batch. My entity has no identifier generation. So what is the problem? Why can't I insert batch?

My Entity class are

  @Entity
  public class Person {

   @Id
   public String name;

    public String getName() {
    return name;
     }

  public void setName(String name) {
    this.name = name;
  }




 }

1 Answers1

3

Very probably you are actually using batching, it is just that Hibernate prints sql for each statement.

To check this, enable DEBUG log level for org.hibernate package (and TRACE level for org.hibernate.type if you want to see bound variables), then check if following appears in the log:

  • Reusing batch statement

  • Executing batch size

If a number larger than 1 is printed for executed batch size, then you are using batching.

Specific to MySQL, to make sure that MySQL driver rewrites insert statements, enable profileSQL parameter in connection url as described here.

NOTE: JDBC batching is disabled if IDENTITY id generator is used.

I also suggest that you consider setting hibernate.order_updates to true, to improve batching of update statements as well. A nice summary about this Hibernate tuning is described here.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110