0

I'd like to configure hibernate batch_size. I configured it in my hibernate configuration as follows:

      configuration.setProperty("hibernate.jdbc.batch_versioned_data", 
 "true");
      configuration.setProperty("hibernate.jdbc.batch_size", "100");
      configuration.setProperty("hibernate.jdbc.fetch_size", "400");
      configuration.setProperty("hibernate.order_inserts", "true");
      configuration.setProperty("hibernate.update_inserts", "true");

I created a test table in my db called TEST with just one integer field. The related entity is:

@Entity
@Table(name = "Test")
public class TestEntity implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "Field")
    private int field;
}

I wrote a very sample test method to create some data:

 tx = session.beginTransaction();
 for (int i = 0; i < 1000; i++) {
   TestEntity test = new TestEntity(i);
   session.save(test);
 }

 tx.commit();

But in hibernate output the insert are performed one by one:

Hibernate: 
insert 
into
    Test
    (Field) 
values
    (?)
Hibernate: 
insert 
into
    Test
    (Field) 
values
    (?)
Hibernate: 
insert 
into
    Test
    (Field) 
values
    (?)
Hibernate: 
insert 
into
    Test
    (Field) 
values
    (?)
Hibernate: 
insert 
into
    Test
    (Field) 
values
    (?)
Hibernate: 
insert 
into
    Test
    (Field) 
values
    (?)
.......
.......

I'm not really understanding why it isn't working.

Any ideas?

elzooilogico
  • 1,659
  • 2
  • 17
  • 18
Simone
  • 29
  • 3
  • See https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/batch.html – OldProgrammer Jun 12 '17 at 11:19
  • It may be that you are actually using batching, bat the SQL statements are just printed separately. Take a look at [this](https://stackoverflow.com/a/30986260/4754790) and [this](https://stackoverflow.com/a/35794220/4754790) answers. – Dragan Bozanovic Jun 12 '17 at 11:36
  • This question have no relation with the `batch-file` tag, so I suggest you to remove it. You may review the description of this tag hoovering the mouse over it. People should not insert such a tag in any question that have any "batch" on it... **`:(`** – Aacini Jun 12 '17 at 13:32
  • @DraganBozanovic you're right. I just traced the time taken to insert the values with and without the batch_size parameter set and it's actually working and it's improving the performance. Anyway it's odd that hibernate didn't print the real SQL used to insert the data – Simone Jun 12 '17 at 15:54

0 Answers0