How to create an INSERT IGNORE query in Hibernate?
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{
...
}
if you are using a simple SQL query, use following code.
Query query = session.createSQLQuery("INSERT IGNORE INTO user (name,username) VALUES (:name,:username)");
query.setParameter("name", name);
query.setParameter("username", username);
int i= query.executeUpdate();