为IDatabaseConnection(HSQLDB)设置数据库配置属性时出错 [英] Error setting database config property for IDatabaseConnection (HSQLDB)

查看:494
本文介绍了为IDatabaseConnection(HSQLDB)设置数据库配置属性时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面包括了完全可测试的代码,当提供包含空字段的数据集 xml 时,它将产生以下错误。以下是示例 dataset.xml

I've included fully testable code below, which generates the following error when supplied with a dataset xml containing empty fields. A sample dataset.xml is also below.


java.lang.IllegalArgumentException: table.column = places.CITY值为
为空,但必须包含一个值(要禁用此功能检查,请将
DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS设置为true)

java.lang.IllegalArgumentException: table.column=places.CITY value is empty but must contain a value (to disable this feature check, set DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS to true)

线程此处相似,但不同,因为它使用了多个 dbTester.getConnection(),而我的代码只使用一个,但有相同的错误。主要问题与此行有关。 databaseConfig.setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS,Boolean.TRUE);

似乎被完全忽略了。我尝试将 init 代码放入 @Test 方法中,但错误仍然存​​在。

The thread here is similar but is different since it uses multiple dbTester.getConnection() whereas my code only uses one, yet has the same error. The main problem relates to this line databaseConfig.setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, Boolean.TRUE); .
It seems to be ignored entirely. I've tried putting the init code inside the @Test method but the error remains.

dataset.xml

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <places address="123 Up Street" city="Chicago" id="001"/>
  <places address="456 Down Street" city="" id="002"/>
  <places address="789 Right Street" city="Boston" id="003"/>
</dataset>

代码:

import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DBConnectionIT {
    IDatabaseTester databaseTester = null;
    IDatabaseConnection iConn = null;
    Connection connection = null;

    @Before
    public void init() throws Exception {
        databaseTester = new JdbcDatabaseTester(org.hsqldb.jdbcDriver.class.getName(), "jdbc:hsqldb:mem:testdb;sql.syntax_pgs=true", "sa", "");
        iConn = databaseTester.getConnection();
        DatabaseConfig databaseConfig = iConn.getConfig();
        databaseConfig.setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, Boolean.TRUE);
        connection = iConn.getConnection();
        createTable(connection);
        IDataSet dataSet = new FlatXmlDataSetBuilder().build(new File("dataset.xml"));
        databaseTester.setDataSet(dataSet);
        databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
        databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL);
        databaseTester.onSetup();
    }

    @Test
    public void testDBUnit() {
        try {
            PreparedStatement pst = connection.prepareStatement("select * from places");
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void createTable(Connection conn) throws Exception {
        PreparedStatement pp = conn.prepareStatement(
                "CREATE TABLE PLACES" +
                        "(address VARCHAR(255), " +
                        "city TEXT, " +
                        "id VARCHAR(255) NOT NULL primary key)");
        pp.executeUpdate();
        pp.close();
    }
}






编辑(基于CésarRodríguez的回答):



我现在在父类中重构了该方法:


EDIT (based on César Rodríguez's answer):

I've now refactored out this method in the parent class:

 protected void setUpDatabaseConfig(DatabaseConfig databaseConfig) {
        databaseConfig.setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, Boolean.TRUE);
    }

并创建了一个子类,该子类 @Overrides 此方法,但表示未使用此子类。如何在父类中解决此类(DBConnectionOverride),以解决我的问题?

and created a sub-class which @Overrides this method, but it's saying this sub-class is not being used. How do I address this class (DBConnectionOverride) in the parent class, to solve my problem?

class DBConnectionOverride extends DBConnectionIT {
    @Override
    protected void setUpDatabaseConfig(DatabaseConfig databaseConfig) {
        databaseConfig.setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, true);
    }
}


推荐答案

I偶然发现了正确的答案,至少是解决了我的问题的答案。一直与 databaseTester.onSetup()这行有关,可以简单地将其替换为 DatabaseOperation.CLEAN_INSERT.execute(iConn,dataSet); 。随意评论为什么这似乎可以修复错误。

I've stumbled upon the correct answer, at least the one which solves my problem. It related to this line all along databaseTester.onSetup() which could simply be replaced with DatabaseOperation.CLEAN_INSERT.execute(iConn, dataSet);. Feel free comment on why this seemed to have fixed the error.

这篇关于为IDatabaseConnection(HSQLDB)设置数据库配置属性时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆