0

最近我把我们SpringBoot项目中的H2数据库从1.4.200版本升级到了2.1.210版本。它仅用于测试目的。对于集成测试,我使用的是 dbunit 2.4.9 hibernate 5.3.10。

Hibernate persistence.xml 如下所示

<persistence-unit name="PUTest"  transaction-type="RESOURCE_LOCAL">

    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <class>**.**..EmailConfigDomain</class>

    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
      <!-- Properties for Hibernate -->
      <property name="hibernate.hbm2ddl.auto" value="create-drop" />
      <property name="hibernate.show_sql" value="false" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
      <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
      <property name="hibernate.connection.password" value="sa"/>
      <property name="hibernate.connection.url" value="jdbc:h2:mem:testdb"/>
      <property name="hibernate.connection.username" value="sa"/>
      <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />
      <property name="net.sf.ehcache.configurationResourceName" value="/ehcache_test.xml" />
      <property name="hibernate.cache.use_second_level_cache" value="true" />
      <property name="hibernate.cache.use_query_cache" value="true" />
      <property name="hibernate.generate_statistics" value="true"/>
    </properties>
  </persistence-unit>

</persistence>

DbUnitTest.java

public abstract class DBUnitTest {

    protected enum PERSISTENCE_UNIT {
        PUTest, test,test, test,test,test, test
    }

    protected List<String> testData = new ArrayList<>();

    protected EntityManagerFactory emFactory;

    private DBUnitDataSetupHelper dataSetupHelper;

    protected String entityManagerName = PERSISTENCE_UNIT.PUTest.name();

    protected EntityManager entityManager;

    protected boolean disableReferentialIntegrityDuringSetup = false;
    private boolean referentialIntegrityWasDisabled = false;

    private IDatabaseConnection connection;

    @Before
    public void setup() throws Exception {
        emFactory = Persistence.createEntityManagerFactory(entityManagerName);

        setEntityManager();

        MockitoAnnotations.initMocks(this);

        connection = (new DatabaseConnection(((SessionImpl) entityManager.getDelegate()).connection()));

        connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());

        if (disableReferentialIntegrityDuringSetup) {
            executeQuery(connection, "SET REFERENTIAL_INTEGRITY FALSE;");
        }

        try {
            for (String aData : testData) {
                URL data = getClass().getClassLoader().getResource(aData);
                FlatXmlDataSetBuilder flatXmlDataSetBuilder = new FlatXmlDataSetBuilder();
                flatXmlDataSetBuilder.setColumnSensing(true);
                IDataSet dataSet = flatXmlDataSetBuilder.build(data);
                DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
            }
        } finally {
            if (referentialIntegrityWasDisabled) {
                executeQuery(connection, "SET REFERENTIAL_INTEGRITY TRUE;");
            }
        }

    }

    private void executeQuery(IDatabaseConnection connection, String sql) throws SQLException {
        Statement stmt = connection.getConnection().createStatement();
        stmt.execute(sql);
        referentialIntegrityWasDisabled = true;
    }

    protected void setEntityManager() {
        entityManager = emFactory.createEntityManager();
    }

    protected void beginTransaction() {
        entityManager.getTransaction().begin();
    }

    protected void commitTransaction() {
        entityManager.getTransaction().commit();
    }

    protected void rollbackTransaction() {
        entityManager.getTransaction().rollback();
    }

    protected DBUnitDataSetupHelper useDataSetupHelper() {
        if (dataSetupHelper == null) {
            dataSetupHelper = new DBUnitDataSetupHelper(this);
        }
        return dataSetupHelper;
    }

    @After
    public void tearDown() throws Exception {
        connection.close();
        System.gc();
    }
}

数据集

<dataset>
    <email_config ui_config_code="test" region="test" name="test" value="test@test.com" created_datetime="date" last_updated_datetime="date" last_updated_user="testuser"/>
</dataset>

升级后我总是在尝试执行时收到此错误消息

  DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);``` 


错误,org.dbunit.database.DatabaseDataSet,在 tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[CONSTANTS,ENUM_VALUES,INDEXES,INDEX_COLUMNS,INFORMATION_SCHEMA_CATALOG_NAME,IN_DOUBT,LOCKS,QUERY_STATISTICS,RIGHTS,ROLES 中找不到表“email_config” , SESSIONS, SESSION_STATE, SETTINGS, SYNONYMS, USERS], _tableMap={ENUM_VALUES=null, CONSTANTS=null, RIGHTS=null, SESSION_STATE=null, SETTINGS=null, INFORMATION_SCHEMA_CATALOG_NAME=null, QUERY_STATISTICS=null, INDEX_COLUMNS=null, ROLES= null, SESSIONS=null, IN_DOUBT=null, LOCKS=null, SYNONYMS=null, USERS=null, INDEXES=null}, _caseSensitiveTableNames=false] 2022-02-20T10:51:53.756, main, , , , , ERROR, org .dbunit.database.DatabaseDataSet,表 'email_config' 在 tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[CONSTANTS, ENUM_VALUES, INDEXES,INDEX_COLUMNS、INFORMATION_SCHEMA_CATALOG_NAME、IN_DOUBT、LOCKS、QUERY_STATISTICS、RIGHTS、ROLES、SESSIONS、SESSION_STATE、SETTINGS、同义词、用户]、_tableMap={ENUM_VALUES=null、CONSTANTS=null、RIGHTS=null、SESSION_STATE=null、SETTINGS=null、INFORMATION_SCH =null,QUERY_STATISTICS=null,INDEX_COLUMNS=null,ROLES=null,SESSIONS=null,IN_DOUBT=null,LOCKS=null,SYNONYMS=null,USERS=null,INDEXES=null},_caseSensitiveTableNames=false]索引=null},_caseSensitiveTableNames=false]索引=null},_caseSensitiveTableNames=false]

4

1 回答 1

0

我删除了这一行

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

并使用休眠连接创建了 sql 表。

于 2022-02-26T15:26:48.280 回答