简体   繁体   中英

Hibernate batch insert oracle 10g

I want to perform a hibernate batch insert using HibernateTemplate but I am not able to find the correct jar containing this class. I know that it should present in spring.jar but not able to find out where to download the spring.jar. For getting this jar, I tried with downloading the entire spring framework also but unfortunately did not found anything like spring.jar in the downloaded package. May be the name is changed, can you please let me know in which jar can I find this HibernateTemplate class??

Also, shall we use the HibernateTemplate class for batch insert in hibernate or there are some other better alternative for batch insert in hibernate?

Regards,

It is not recommended to use Hibernate for batch processing. Unless you already have a business layer built with Hibernate, and you want to reuse some code, you should do your batch processing in plain old JDBC, perhaps managed by a framework like Spring Batch.

If you want to use Hibernate, then you should use a StatelessSession .

There's no need for any HibernateTemplate since the Session already provides you everything you need.


Edit: you can handle transactions:

Session session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();
// DO SOMETHING
tx.commit();
session.close();

By the way if you create a transaction for 10 items then I guess you want to split your treatment into multiple transactions of 10 insertions. Is transaction really needed? Because anyway one transaction can fail while the others do not, and that leaves you with some inserted entities, and some that are not.

If you want atomicity of your batch treatment (all inserted, or none inserted) you must run the transaction for all the elements, and not only 10 of them. But take care to not lock your database... I don't think it's a good idea to use a big long running transaction for a batch. Instead you probably should compensate the insertion failures on your own.

The HibernateTemplate class is part of org.springframework.orm-3.1.3.RELEASE.jar which is contained in the spring-framework-3.1.3.RELEASE.zip

If you already use the spring framework in your project there is no reason why you should not use the HibernateTemplate for batch updates. The advantage of the HibernateTemplate is that you don't have to care about Hibernate Sessions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM