mysql - 从查询创建数据集时,DBUnit 忽略列别名?

标签 mysql dbunit

我有两个数据集,我想将它们与 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 数据库。

最佳答案

这看起来像是由 change in the MYSQL Java connector 引起的

由于 DatabaseTableMetaData 调用 ResultSetMetaData#g​​etColumnName() 而不是 #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 .

关于mysql - 从查询创建数据集时,DBUnit 忽略列别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15159085/

相关文章:

MySQL VB.Net 连接问题

c# - 主动拒绝 C# 与 Ubuntu MySQL 服务器的连接

mysql - 向 mysql 正则表达式添加一个点

java - DbUnit Oracle 中的 NoSuchTableException

postgresql - 带 Postgresql 的 DBUnit 和列名区分大小写 - 它是如何工作的?

java - 尝试加载 CLOB 字段时,DBunit 生成 java.lang.ClassCastException : java. lang.String 无法转换为 oracle.sql.CLOB

mysql连接数据库不监听端口?

php - 需要更新数据库中的一行

java - Oracle + dbunit 获取 AmbiguousTableNameException

java - MySQL 增量列和 DBunit 数据集的问题