• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Avoid duplication while insert through Hibernate

 
Ranch Hand
Posts: 64
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am inserting data into a table daily. Suppose today i have inserted a row with values x,y and z , Suppose tomorrow again i get same data x,y and z at this timei dont want to insert this data into DB. How can we do it in easiest way using hibernate.
My goal is to avoid duplicate rows .
Your quick response will be appreciated.

Thanks
Shadab.
 
Rancher
Posts: 4116
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a UNIQUE constraint to the table.
 
mohammad shadab
Ranch Hand
Posts: 64
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave,

let say my table has four columns and first column is a serial , suppose i entered a row with values john,english,UK  and again some one try to insert a row with values john,english,UK . So how i can avoid this insert in HIBERNATE.
What is the best way to stop this 2nd time insert for same data.

Thanks
Shadab.
 
Dave Tolls
Rancher
Posts: 4116
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For MySQL you would do something like:
ADD UNIQUE 'my_index'('column1', 'column2', 'column3)

The first place to ensure this is the db itself.

For Hibernate you would use the @UniqueConstraint annotation for the table, which should add the equivalent of the above when it creates the table.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!