0

Recently I have upgraded H2 database in our SpringBoot project from version 1.4.200 to 2.1.210. It is used for testing purposes only. For integration testing I am using dbunit 2.4.9 hibernate 5.3.10.

Hibernate persistance.xml looks like following

<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

<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>

After upgrading I always get this errormessage when trying execute

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


ERROR, org.dbunit.database.DatabaseDataSet, Table 'email_config' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[CO NSTANTS, ENUM_VALUES, INDEXES, INDEX_COLUMNS, INFORMATION_SCHEMA_CATALOG_NAME, IN_DOUBT, LOCKS, QUERY_STATISTICS, RIGHTS, ROLES, SESSIONS, SESSION_STATE, SETTINGS, SYNONYMS, USERS], _t ableMap={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, Table 'email_config' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[CO NSTANTS, ENUM_VALUES, INDEXES, INDEX_COLUMNS, INFORMATION_SCHEMA_CATALOG_NAME, IN_DOUBT, LOCKS, QUERY_STATISTICS, RIGHTS, ROLES, SESSIONS, SESSION_STATE, SETTINGS, SYNONYMS, USERS], _t ableMap={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]

Mr.Rajeh
  • 73
  • 1
  • 5

1 Answers1

0

I removed this line

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

and created the sql table using hibernate connection.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Mr.Rajeh
  • 73
  • 1
  • 5