• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ignoring insert errors while inserting in hibernate

 
raj kothari
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application, I use for-loop and save many objects. Outside for-loop, I commit. I have unique index set in table with some columns. If there is duplicate record tried to insert, hibernate throws exception. I want to avoid getting exception and just continue with other unique records available.
How can I do this?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just commit after every insert.
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just commit after every insert.

just commit after the insert may not be enough, because, if you get the exception once, rest of the records wont be processed.
To avoid this, you could catch the hibernate exception type which comes when you insert duplicate record , ignore the exception and go ahead..
 
raj kothari
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Babbar wrote:
Just commit after every insert.

just commit after the insert may not be enough, because, if you get the exception once, rest of the records wont be processed.
To avoid this, you could catch the hibernate exception type which comes when you insert duplicate record , ignore the exception and go ahead..


It is not possible. I can catch the exception but cannot continue. Because I commit outside for-loop. So, when exception is thrown, it means none of the records got inserted, I can catch the exception, but my problem still there.
I would like to have some option in hibernate if possible rather than doing something in java try-catch stuff. The option should tell hibernate to continue inserting records even if any single record has some issue in the batch of many records coming in with single commit.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is not possible. I can catch the exception but cannot continue. Because I commit outside for-loop

Then commit inside the loop.


I would like to have some option in hibernate if possible rather than doing something in java try-catch stuff. The option should tell hibernate to continue inserting records even if any single record has some issue in the batch of many records coming in with single commit.

Transactional operations don't work like this (in Hibernate or any other database backed app). If you've defined your unit of work as all objects in the collection its all or nothing. If you define it as individual objects then any unsuccessful operations will not affect the other transactions.
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not possible. I can catch the exception but cannot continue. Because I commit outside for-loop. So, when exception is thrown, it means none of the records got inserted,


You are not correct here...you can catch the exception and not do anything...Hibernate will commit the other records....

The below test which i wrote runs perfectly fine and inserts 9 records in the database, although i am inserting each record twice...

 
raj kothari
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. This is very good. We have generally not used persist() so far in application. We will try based on your example.
Thanks to you and Paul trying to help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic