1

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,

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

yes, I tried using HibernateTemplate but facing problems such as Transaction is not working with HibernateTemplate. Will Transaction will work with StatelessSession? Reason is, if there are 10 records to be inserted and the fifth one fails, then I would prefer not to insert any of the records rather than inserting four records. This kind of requirement will be taken care by Transaction if I use StatelessSession right? Please let me know about this.
StatelessSession can handle transactions.
What is the probable option for compensating the insertion failures then? Can you please suggest something on this?
Restarting the batch for the failed insertions, mark them has "notInserted" or something like that. Hard to say if you don't explain why you need atomicity.
0

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

7 Comments

No, using spring framework is not an option for our project, only for achieveing the batch insert we are planning to use it. Hence I asked is there any alternative of hibernate batch insert other than spring framework?
@user182944 Hibernate Batch Processing is independent from the SpringFramework. The HibernateTemplate is only a helper which simplifies the data access code. And I've only answered your question. There is a Chapter about batch updates in the Hibernate Documentation Chapter 13. Batch Processing. Example Hibernate: Bulk Insert/Batch Insert. Google is your friend.
I tried using HibernateTemplate, facing one difficulty. Suppose there are 10 records to be inserted and the fifth record fails, then the first four are getting executed and once the fifth record fails, the remain insert queries are not getting executed at all. I need two things to know here: 1) Is there some settings which will let either all records get inserted and if any one fails then none of the ten records should be inserted? 2) Even if the fifth record fails, I want to continue with the insertion of remaining records. How to achieve these? Please help
@andih: the documentation of HibernateTemplate itself discourages the use of this class, and suggests using the native Hibernate API directly. So there's a good reason not to use HibernateTemplate: it doesn't add much value, and doesn't even exist anymore for Hibernate 4.
@JBNizet it has some advantages when you use the Springframework Transactionmanagement with another TransactionManager than the HibernateTransactionManager. I see no advantage for Batch Updates but also no reason not to use it if the springframework is already used in the project.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.