tags:

views:

357

answers:

1

I am having trouble creating an export of my database using an org.dbunit.database.QueryDataSet. When I call org.dbunit.dataset.xml.FlatXmlDataSet.write(IDataSet, OutputStream), I get the following stack trace:

java.lang.IllegalStateException: Did not find column 'MYCOL' for <schema.table> 'MYSCHEMA  .MYTABLE' in catalog 'MYDB' because names do not exactly match.
    at org.dbunit.database.ResultSetTableMetaData.scrollTo(ResultSetTableMetaData.java:297)
    at org.dbunit.database.ResultSetTableMetaData.createColumnFromDbMetaData(ResultSetTableMetaData.java:262)
    at org.dbunit.database.ResultSetTableMetaData.createMetaData(ResultSetTableMetaData.java:154)
    at org.dbunit.database.ResultSetTableMetaData.createMetaData(ResultSetTableMetaData.java:131)
    at org.dbunit.database.ResultSetTableMetaData.<init>(ResultSetTableMetaData.java:97)
    at org.dbunit.database.AbstractResultSetTable.<init>(AbstractResultSetTable.java:84)
    at org.dbunit.database.AbstractResultSetTable.<init>(AbstractResultSetTable.java:63)
    at org.dbunit.database.ForwardOnlyResultSetTable.<init>(ForwardOnlyResultSetTable.java:65)
    at org.dbunit.database.CachedResultSetTableFactory.createTable(CachedResultSetTableFactory.java:52)
    at org.dbunit.database.AbstractDatabaseConnection.createQueryTable(AbstractDatabaseConnection.java:90)
    at org.dbunit.database.AbstractDatabaseConnection.createTable(AbstractDatabaseConnection.java:115)
    at org.dbunit.database.QueryTableIterator.getTable(QueryTableIterator.java:143)
    at org.dbunit.dataset.stream.DataSetProducerAdapter.produce(DataSetProducerAdapter.java:83)
    at org.dbunit.dataset.xml.FlatXmlWriter.write(FlatXmlWriter.java:124)
    at org.dbunit.dataset.xml.FlatXmlDataSet.write(FlatXmlDataSet.java:341)

In researching this, I saw that someone else had this problem back in February, and fixed it using a snapshot build of 2.4.4. I am using the regular release build of 2.4.4.

Any ideas?

+1  A: 

Hi,

This seems to be a bug in the DB2 driver.

If you look at your log, you have a spare space in the schema name. This schema name is returned by the DB2 driver. A fix as been implemented for this specific error in the last source of dbunit (bug 2838922).

As mentioned in the bug 2838922, this might not solve the issue completely.

If this is the case, a workaround for the remaining problem is to implement your own IMetadataHandler by taking the default one as an example and changing the "matches" method as follow:

    ...
    boolean areEqual =
            areEqualIgnoreBothNull(catalog, catalogName, caseSensitive) &&
            areEqualIgnoreNull(schema, schemaName, caseSensitive) &&
            areEqualIgnoreNull(table, tableName, caseSensitive) &&
            areEqualIgnoreNull(column, columnName, caseSensitive);

    return areEqual;


    private boolean areEqualIgnoreBothNull(String value1, String value2,
                                       boolean caseSensitive) {
            boolean areEqual = true;
            if (value1 != null && value2 != null) {
                if (value1.equals("") && value2.equals("")) {
                    if (caseSensitive) {
                        areEqual = value1.equals(value2);
                    } else {
                        areEqual = value1.equalsIgnoreCase(value2);
                    }
                }
            }
            return areEqual;
        }

(you can also solve the first schema name issue in your own IMetadataHandler by triming the schema name before the equality test)

The page http://www.dbunit.org/properties.html shows how to replace a the implementation of IMetadataHandler.

Hoping this helps,

Matthieu

matthieus