最近我将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);```
错误,在tableMap=org.dbunit.dataset中找不到org.dbnit.database.DatabaseDataSet表"email_config"。OrderedTableNameMap[_tableNames=[CONSTANTS、ENUM_VALUES、INDEXS、INDEX_COLUMNS、INFORMATION_SCHEMA_CATALOG_NAME、IN_DOUBT、LOCKS、QUERY_STATISTICS、RIGHTS、ROLES、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,INDEXE=null},_caseSentiveTableNames=false]2022-02-20T10:51:53.756,main,,,ERROR,org.dbuit.database.DatabaseDataSet,在tableMap=org.dbunit.dataset中找不到表"email_config"。OrderedTableNameMap[_tableNames=[CONSTANTS、ENUM_VALUES、INDEXS、INDEX_COLUMNS、INFORMATION_SCHEMA_CATALOG_NAME、IN_DOUBT、LOCKS、QUERY_STATISTICS、RIGHTS、ROLES、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,INDEXE=null},_caseSentiveTableNames=false]
我删除了这行
PD_6并使用hibernate连接创建了sql表。