【问题标题】:DbUnit - Delete rows based on non-primary key field valueDbUnit - 根据非主键字段值删除行
【发布时间】:2011-09-03 01:26:26
【问题描述】:

看起来 DbUnit 正在使用 JDBC 元数据来确定主键字段并使用这些字段构造删除语句:

delete from tbl_name where pk_field1=? and pk_field2=? and pk_field3=?

有没有办法根据复合键的一个字段值或非主键字段的值删除行(例如,删除 created_date = xyz 的行)

【问题讨论】:

    标签: junit dbunit


    【解决方案1】:

    您可以创建一个 QueryDataSet 并将 DatabaseOperation 设置为 DELETE。

    例如,如果您正在扩展 DBTestCase:

    protected IDataSet getDataSet() {
        QueryDataSet queryDataSet = null;
        String query = "SELECT * FROM tbl_name pk_field1=? and pk_field2=?";
        try {
            queryDataSet = new QueryDataSet(super.getConnection());
            queryDataSet.addTable("tbl_name",query);
        } catch (Exception e) {
            e.printStackTrace();            
        }
        return queryDataSet;
    }
    
    protected DatabaseOperation getSetUpOperation() throws Exception {
        return DatabaseOperation.DELETE;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-21
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多