How to create an INSERT IGNORE query in Hibernate?

后端 未结 2 1434
终归单人心
终归单人心 2021-01-11 10:41

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 IN

相关标签:
2条回答
  • 2021-01-11 11:20

    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();
    
    0 讨论(0)
  • 2021-01-11 11:40

    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{
       ...
    }
    
    0 讨论(0)
提交回复
热议问题