为什么DBUnit无法使用DatabaseUnitRuntimeException从MySQL导出完整数据集?

时间:2016-01-21 16:10:12

标签: java mysql jdbc dbunit

我正在尝试开始使用DBUnit来测试我的Java webapp中的DAO层。

我想要做的第一件事是从我的dev数据库生成一个完整的导出到XML数据集,所以我可以使用它来填充要在测试中使用的内存数据库。

我跟着the example on DBUnit's website,但它失败了,导致了一个空的XML文件。

一个表的导出工作正常。

以下是执行导出的代码:

public class DBUnitDatasetExtractor {

    public static final String DRIVER = "com.mysql.jdbc.Driver";
    public static final String JDBC_URL = "jdbc:mysql://192.168.56.4:3306/myschema";
    public static final String SCHEMA = "myschema";
    public static final String USER = "myuser";
    public static final String PASSWORD = "password";

    public static void main(String[] args) throws Exception
    {
        Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
        root.setLevel(Level.DEBUG);

        // database connection
        Class driverClass = Class.forName(DRIVER);
        Connection jdbcConnection = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
        IDatabaseConnection connection = new DatabaseConnection(jdbcConnection, SCHEMA);

        // partial database export - works
        QueryDataSet partialDataSet = new QueryDataSet(connection);
        partialDataSet.addTable("desk");
        FlatXmlDataSet.write(partialDataSet, new FileOutputStream("partial.xml"));

        // full database export - does not work
        IDataSet fullDataSet = connection.createDataSet();
        FlatXmlDataSet.write(fullDataSet, new FileOutputStream("full.xml"));
    }

}

我做错了什么?

我将记录器级别设置为debug,所以这是相关的跟踪:

16:41:34.551 [main] DEBUG o.d.database.DatabaseTableMetaData - getColumns() - start
16:41:35.548 [main] DEBUG org.dbunit.util.SQLHelper - isEscaped returns 'false' for tableName=desk (dbIdentifierQuoteString=`)
16:41:35.549 [main] DEBUG org.dbunit.util.SQLHelper - Database does not store upperCase or lowerCase identifiers. Will not correct case of the table names.
16:41:35.549 [main] DEBUG o.d.database.DatabaseTableMetaData - Validating if table 'desk' exists in schema 'tango' ...
16:41:35.550 [main] DEBUG org.dbunit.util.SQLHelper - close(resultSet=com.mysql.jdbc.JDBC42ResultSet@28eaa59a) - start
16:41:35.550 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - update(tableName=desk, object=table=desk, cols=[], pk=[]) - start
16:41:36.552 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - getTableName(tableName=desk) - start
16:41:36.552 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - getTableName(tableName=desk) - end - result=DESK
16:41:36.552 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - getTableName(tableName=desk) - start
16:41:36.552 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - getTableName(tableName=desk) - end - result=DESK
Disconnected from the target VM, address: '127.0.0.1:59104', transport: 'socket'
Exception in thread "main" org.dbunit.DatabaseUnitRuntimeException: At least one column is required to build a valid select statement. Cannot load data for table=desk, cols=[], pk=[]
    at org.dbunit.database.DatabaseDataSet.getSelectStatement(DatabaseDataSet.java:127)
    at org.dbunit.database.AbstractResultSetTable.getSelectStatement(AbstractResultSetTable.java:134)
    at org.dbunit.database.AbstractResultSetTable.<init>(AbstractResultSetTable.java:105)
    at org.dbunit.database.ForwardOnlyResultSetTable.<init>(ForwardOnlyResultSetTable.java:59)
    at org.dbunit.database.CachedResultSetTableFactory.createTable(CachedResultSetTableFactory.java:60)
    at org.dbunit.database.DatabaseDataSet.getTable(DatabaseDataSet.java:319)
    at org.dbunit.database.DatabaseTableIterator.getTable(DatabaseTableIterator.java:89)
    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:378)
    at ch.unine.tango.persistence.DBUnitDatasetExtractor.main(DBUnitDatasetExtractor.java:46)

这是数据库信息:

17:02:28.007 [main] DEBUG o.dbunit.database.DatabaseConnection - Database connection info: 
    database product name=MySQL
    database version=5.1.73-1
    database major version=5
    database minor version=1
    jdbc driver name=MySQL Connector Java
    jdbc driver version=mysql-connector-java-5.1.38 ( Revision: fe541c166cec739c74cc727c5da96c1028b4834a )
    jdbc driver major version=5
    jdbc driver minor version=1

编辑:这是桌面表的DDL:

CREATE TABLE desk
(
    type VARCHAR(31) NOT NULL,
    id BIGINT PRIMARY KEY NOT NULL,
    creation_date DATETIME NOT NULL,
    creation_user VARCHAR(255) NOT NULL,
    update_date DATETIME NOT NULL,
    update_user VARCHAR(255) NOT NULL,
    version BIGINT NOT NULL,
    name VARCHAR(50) NOT NULL,
    location_id BIGINT NOT NULL,
    user_id BIGINT,
    CONSTRAINT fk_desk_to_location FOREIGN KEY (location_id) REFERENCES location (id),
    CONSTRAINT fk_desk_to_user FOREIGN KEY (user_id) REFERENCES tango_user (id) ON DELETE CASCADE
);
CREATE UNIQUE INDEX location_id ON desk (name);
CREATE INDEX idx_desk_location_id ON desk (location_id);
CREATE INDEX idx_desk_user_id ON desk (user_id);

EDIT2:我针对SQL Server数据库运行相同的程序,该数据库与MySQL的数据库结构相同(它用于同一个应用程序),它运行得非常好。这个问题似乎与MySQL有关。

0 个答案:

没有答案