• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Ignoring insert errors while inserting in hibernate

 
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?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just commit after every insert.
 
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
Hibernate Eclipse IDE 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.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic