We're updating the issue view to help you get more done. 

Incorrect batch inserts example

Description

http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#batch-session-batch-insert

1 2 3 4 5 6 7 8 9 10 for ( int i = 0; i < entityCount; ++i ) { Person Person = new Person( String.format( "Person %d", i ) ); entityManager.persist( Person ); if ( i > 0 && i % batchSize == 0 ) { //flush a batch of inserts and release memory entityManager.flush(); entityManager.clear(); } }

The first batch size will be batchSize+1, I think it should be

1 2 3 4 5 6 7 8 9 10 for ( int i = 0; i < entityCount; i++ ) { Person Person = new Person( String.format( "Person %d", i ) ); entityManager.persist( Person ); if ( (i + 1) % batchSize == 0 ) { //flush a batch of inserts and release memory entityManager.flush(); entityManager.clear(); } }

And early version http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#batch

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0 ) { //20, same as the JDBC batch size //flush a batch of inserts and release memory: session.flush(); session.clear(); } } tx.commit(); session.close();

First batch size will be 1, mislead lots of people such as https://stackoverflow.com/questions/7349464/bulk-insert-or-update-with-hibernate

Environment

None

Status

Assignee

Vlad Mihalcea

Reporter

Yanming Zhou

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.2.15

Priority

Major