Take the 2-minute tour ×

Right now I am using entitymanager to insert list of object entity to database with my dao using code like this.

public void insertBatch(List<EntityObject> o){
    for(int i=0;i<o.size();i++){
         if(o.size % 100 == 0){ //equal to JDBC batch size

And when I monitor the sql statement by add this into application-context

    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">

And the result in console will look like this

Hibernate: insert into TABLE (FIELD1, FIELD2) values (?, ?)
...//untill reach 100 lines.

My question is. Does it mean that each time when i call this method it will do round trip with database for 100 times,or is it do only 1 round trip, or else?

Any answer or insight would be appreciate.


share|improve this question
Just add a log statement before the call to flush() and you'll see by yourself. –  JB Nizet May 8 '12 at 7:52
add comment

1 Answer

up vote 1 down vote accepted

If you need batching in hibernate jpa,try setting

<property name="hibernate.jdbc.batch_size" value="100" />

As per JPA spec:

The managed entity X will be entered into the database at or before transaction commit or as a result of the flush operation.

So, flush is the database round trip.

BTW if you are using hibernate-jpa with spring (transaction), then spring will manage transactions for you. You need not be calling em.flush (ideally)

share|improve this answer
add comment

Your Answer


