How ignore duplicate rows when insert

前端 未结 1 1788
余生分开走
余生分开走 2021-01-26 16:46

I use hibernate-jpa-2.1-api. And I need some functionality.

I parse a file every minute and insert data into MSSQL DB. I need to skip duplicate rows. For e

相关标签:
1条回答
  • 2021-01-26 17:36

    Database style option

    Hibernate does not offer to add an option to its insert into statements. And I don't know if the same option is available for MS SQL.

    But if you find such an option, you can intercept the insert statement and add that yourself:

    public class IgnoreRowOnDupInterceptor extends EmptyInterceptor {
    
      public String onPrepareStatement(String sql) {
        if (sql.startsWith("insert into avaya_cm_cdr") {
          return sql.replace("insert into", 
            "insert /*+ ignore_row_on_dupkey_index(avaya_cm_cdr, i_avaya_cm_cdr_nodub) */ into");
        }
        return sql;
      }
    
    } 
    

    You need to declare this interceptor in your persistence.xml:

    <property name="hibernate.ejb.interceptor" value="...IgnoreRowOnDupInterceptor" />
    

    JPA style option

    You could remember the last line from the last parsing (or retrieve it from the database) and skip the file until that line. In that case you even would save the time to parse every existing item again and again.

    From my point of view this is the JPA way, because you usually use the database only as storage and keep the business logic in the (Java) application.

    0 讨论(0)
提交回复
热议问题