Bulk inserts of data are a type of operation best not performed by Hibernate. For example, a user may have 100,000 records that have to be imported into a single table. Don't use Hibernate for this sort of operation—use your database's built-in import tools instead. The built-in import will be faster than Hibernate (or, for that matter, handwritten JDBC).
If, for some reason, you do need to do a bulk import via Hibernate, take account of the following tips:
- Make sure the hibernate.jdbc.batch_size option (specified in your hibernate.properties, as described in Chapter 6) is turned on and set to a reasonably large value.
- Consider Session.commit() on to break up the transactional overhead. Presumably you will do this only if you are very confident that it will succeed.
- Make sure that you call Session.close() it or Session.clear() after each call to Session.commit(). Otherwise, Hibernate will attempt to maintain the inserted object in the session-level cache.
- Consider the seqhilo or assigned generator to optimize key generation.