Thorsten Niehues
Thorsten Niehues

Reputation: 14452

DbUnit DbComparisonFailure expected:<uniqueidentifier>

We do databas / dataset comparison.

Source DB: SQL Server
Target DB: SAP HANA

Error message:
org.dbunit.assertion.DbComparisonFailure: Incompatible data types: (table=dataset, col=JobId) expected:<uniqueidentifier> but was:<VARCHAR>

Work-around: cast(JobId as varchar(36)) JobId

But why does this error occure? JobId is defined as varchar(36) in the source table
How to make this work (The SQLs are generated and manually adding a cast is cumbersome)

Upvotes: 0

Views: 174

Answers (2)

Thorsten Niehues
Thorsten Niehues

Reputation: 14452

Thanks to Lars Br answer:

I made the following solution:

  1. Remove DbUnit jar from classpath (version 2.5.4)
  2. Add DbUnit source code (also version 2.4.5)
  3. Edit the class org.dbunit.ext.mssql.MsSqlDataTypeFactory
    Method public DataType createDataType(int sqlType, String sqlTypeName) throws DataTypeException

    if(sqlType == Types.CHAR)
    {
        if (UniqueIdentifierType.UNIQUE_IDENTIFIER_TYPE.equals(sqlTypeName))
        {
            return new UniqueIdentifierType();
        }
    }
    

    to

    /* Do not generate UNIQUE_IDENTIFIER_TYPE since it would not match varchar on SAP HANA
    if(sqlType == Types.CHAR)
    {
        if (UniqueIdentifierType.UNIQUE_IDENTIFIER_TYPE.equals(sqlTypeName))
        {
            return new UniqueIdentifierType();
        }
    }*/
    

Upvotes: 1

Lars Br.
Lars Br.

Reputation: 10388

Looking through the google results, this seems to be a common issue with MS SQL Servers UniqueIdentifier column type. See http://dbunit.wikidot.com/mssql.

There, they basically put the type mapping in the DBUnit factory code, instead of a CAST on every column: public class MsSqlDataTypeFactory extends DefaultDataTypeFactory { public static int NVARCHAR = -9; public static int UNIQUEIDENTIFIER = -11;

public DataType createDataType(int sqlType, String sqlTypeName) throws DataTypeException
{
    if (sqlType == NVARCHAR)
    {
        return DataType.VARCHAR;
    }

    if (sqlType == UNIQUEIDENTIFIER)
    {
        return DataType.VARCHAR;
    }

    return super.createDataType(sqlType, sqlTypeName);
 }
}

Upvotes: 2

Related Questions