开发者

DbUnit - Delete rows based on non-primary key field value

It looks like DbUnit is using JDBC metadata to determine the primary key fields and constructing delete statement using th开发者_如何转开发ose field:

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

Is there a way to delete rows based on one field value of a composite key or value of a non-primary key field (e.g. delete rows where created_date = xyz)


You can create a QueryDataSet and set DatabaseOperation to DELETE.

For exemple, if you are extending 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜