【问题标题】:DBUnit ignores column aliases, when creating dataset from query?从查询创建数据集时,DBUnit 忽略列别名?
【发布时间】:2013-03-01 13:36:07
【问题描述】:

我有两个数据集,我想与 DBUnit 进行比较:一个定义为 XML 文件,另一个我正在尝试使用查询创建:

dbConnection.createQueryTable("my_table", "SELECT ...");

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 作为 table2table3 列的名称,而不是查询中定义的 table2_labellabel...

如何解决这个问题?我正在使用 MySQL 数据库。

【问题讨论】:

    标签: mysql dbunit


    【解决方案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

    根据these instructions

    【讨论】:

    • 顺便说一句,我使用的是 DBUnit 2.4.9 和 MySQL Connector 5.1.26
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 2020-04-25
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    相关资源
    最近更新 更多