Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Avoid duplication while insert through Hibernate

 
mohammad shadab
Ranch Hand
Posts: 52
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.
 
Dave Tolls
Ranch Hand
Posts: 2110
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a UNIQUE constraint to the table.
 
mohammad shadab
Ranch Hand
Posts: 52
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
Ranch Hand
Posts: 2110
16
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic