Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save multiple entities in Hibernate?

Tags:

hibernate

jpa

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

like image 392
Rami Avatar asked Aug 20 '12 07:08

Rami


People also ask

How Hibernate saves bulk data?

save(employee); } tx. commit(); session. close(); By default, Hibernate will cache all the persisted objects in the session-level cache and ultimately your application would fall over with an OutOfMemoryException somewhere around the 50,000th row.

Can we save list of objects in Hibernate?

Hibernate provides the facility to persist the collections. A collection can be a list, set, map, collection, sorted set, sorted map. java.


1 Answers

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

  • JPA/Hibernate bulk(batch) insert
  • Batch inserts with JPA/EJB3

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.

like image 86
JMelnik Avatar answered Oct 27 '22 23:10

JMelnik