我有两个数据集,我想与DBUnit进行比较:一个定义为XML文件,另一个我尝试使用查询创建:
dbConnection.createQueryTable("my_table", "SELECT ...");
选择遍历几个表,其中少数表具有相同的列名,因此我使用别名来区分它们。查询运行正常,但似乎在创建数据集时,DBUnit会忽略列别名并使用原始名称。这显然可以在DEBUG日志中看到:
01.03.13 14:18:43,228 [ DEBUG] AbstractDatabaseConnection - createQueryTable(resultName=my_table, sql=
SELECT `table1`.`name`
, `table2`.`label` AS `table2_label`
, `table3`.`label`
FROM `table1`
, `table2`
, `table3`
WHERE `table1`.`id` = `table2`.`table1_id`
AND `table2`.`id` = `table3`.`table2_id`
) - start
01.03.13 14:18:43,228 [ DEBUG] DatabaseDataSourceConnection - getConnection() - start
01.03.13 14:18:43,228 [ DEBUG] DatabaseConfig$Configurator - Statement fetch size set to 100
01.03.13 14:18:43,240 [ DEBUG] DatabaseDataSourceConnection - getConnection() - start
01.03.13 14:18:43,240 [ DEBUG] DatabaseTableMetaData - The 'schemaName' from the ResultSetMetaData is empty-string and not applicable hence. Will not try to lookup column properties via DatabaseMetaData.getColumns.
01.03.13 14:18:43,240 [ DEBUG] DatabaseTableMetaData - The 'schemaName' from the ResultSetMetaData is empty-string and not applicable hence. Will not try to lookup column properties via DatabaseMetaData.getColumns.
01.03.13 14:18:43,240 [ DEBUG] DatabaseTableMetaData - The 'schemaName' from the ResultSetMetaData is empty-string and not applicable hence. Will not try to lookup column properties via DatabaseMetaData.getColumns.
01.03.13 14:18:43,240 [ DEBUG] ForwardOnlyResultSetTable - getValue(row=0, columnName=name) - start
01.03.13 14:18:43,240 [ DEBUG] ForwardOnlyResultSetTable - getValue(row=0, columnName=label) - start
01.03.13 14:18:43,240 [ DEBUG] ForwardOnlyResultSetTable - getValue(row=0, columnName=label) - start
01.03.13 14:18:43,240 [ DEBUG] ForwardOnlyResultSetTable - getValue(row=1, columnName=name) - start
01.03.13 14:18:43,241 [ DEBUG] ForwardOnlyResultSetTable - getValue(row=1, columnName=label) - start
01.03.13 14:18:43,241 [ DEBUG] ForwardOnlyResultSetTable - getValue(row=1, columnName=label) - start
01.03.13 14:18:43,241 [ DEBUG] ForwardOnlyResultSetTable - getValue(row=2, columnName=name) - start
01.03.13 14:18:43,241 [ DEBUG] ForwardOnlyResultSetTable - getValue(row=2, columnName=label) - start
01.03.13 14:18:43,241 [ DEBUG] ForwardOnlyResultSetTable - getValue(row=2, columnName=label) - start
如您所见 - getValue(row=X)
使用label
作为table2
和table3
列的名称而非查询中定义的table2_label
和label
...
如何解决这个问题?我正在使用MySQL数据库。
答案 0 :(得分:1)
这看起来像是由change in the MYSQL Java connector
引起的由DatabaseTableMetaData
拨打ResultSetMetaData#getColumnName()
而不是#getColumnLabel()
您没有说明您正在使用的DBUnit / MySql版本,但DBUnit 2.2& 2.4.9受MySql连接器5.1.26影响。
作为一种解决方法,MySQL开发人员已经包含了一个向后兼容性选项,在上面的更改中进行了讨论。
要配置它,请在JDBC url中添加一个属性:
jdbc:mysql://host/db_name?useOldAliasMetadataBehavior=true