Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an INSERT IGNORE query in Hibernate?

Tags:

java

hibernate

Hibernate has to make inserts in a table which has an unique field. I want to ignore duplicate entries so that my program keeps running. In MySQL I would simply say INSERT IGNORE, but I can't figure out how to do that in Hibernate. Any suggestions? Thanks!

like image 689
Crayl Avatar asked Nov 10 '13 19:11

Crayl


1 Answers

Have you tried using the @SQLInsert annotation ? This way, you can overwrite the Hibernate statement with your own custom SQL and use INSERT IGNORE:

@SQLInsert(sql="INSERT IGNORE INTO CUSTOMER(id,name) VALUES(?,?)")
class Customer{
   ...
}
like image 186
Kai Sternad Avatar answered Oct 07 '22 14:10

Kai Sternad