5

in my Java web application, I would like to persist every 100 instances of the save entity at once, what is the best way to do that? I have some time constraints that I have to respect, the persisting operation should be fast. I there any method faster than the others? Thank you

Rami
  • 8,044
  • 18
  • 66
  • 108

2 Answers2

12

Basically what you are looking for is batch insert into database using JPA. These topics have already been brought up, these will help you:

Summary

  • Using Hibernate's batch inserts. Hiberate provides methods for batch inserting and updating entities.

  • Disabling automatic ID generation. If you generate ids automatically, Hibernate executes query for each entity to generate the primary key.

Opinion

Basically I think that disabling automatic ID generation is a bad idea. You can take most of Hibernate's batch methods, but this will not save you much of performance.

After this is done, I suggest you looking for optimizations in other layers of your application.

Community
  • 1
  • 1
d1e
  • 6,372
  • 2
  • 28
  • 41
  • 1
    I finally try your solution and it works. Thank you very much. – Rami Aug 29 '12 at 12:06
  • @Rami, glad to hear. Which solution did you use eventually? – d1e Aug 29 '12 at 12:54
  • Hey @JMelnik, I am using batch insert now, so instead of saving each entity, I am taking profit of the one-to-may relation and when I am saving a parent entity, all its child are saved with it. Although, I am sill using the automatic ID generation. – Rami Aug 29 '12 at 13:36
1

If you are using Hibernate intergarated with Spring, you can use HibernateTemplate#saveOrUpdateAll

 mHibernateTemplate.saveOrUpdateAll(entities);
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
  • What is the version of this syntax ? Having this code with Hibernate 3.x now that am upgrading to HIbernate 5.x this option is not available. What is the alternative ? – Jess Jun 06 '17 at 19:55
  • incorrect. try to specify version. seems like saveOrUpdateAll is not avaiable – Vishrant Aug 02 '17 at 20:15