title: DbUnit H2 in memory db with Spring and Hibernate
body: <p>Hi I'm trying a little POC with JPA and unit test to verify that the DB schema is created. I'm working with H2 DB and I set to Hibernate create the schema from the entities, but when DbUnit tries to initialize the DB from a dataset I always get a Table ... not found in tableMap. I read that I have to add the property DB_CLOSE_DELAY=-1 to DB URL but is like after Hibernate creates the schema the DB is losted when DbUnit tries to initialize.</p>
  
  <p>Any ideas? Any help is highly appreciated.</p>
  
  <p>This is my config:</p>
  
  <p><strong>application-context.xml</strong></p>
  
  <pre><code>&lt;bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSourceH2" /&gt;
      &lt;property name="packagesToScan" value="com.xxx.model" /&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="generateDdl" value="true" /&gt;
              &lt;property name="showSql" value="true" /&gt;
              &lt;!-- property name="databasePlatform" value="org.hibernate.dialect.MySQLInnoDBDialect" /--&gt;
              &lt;property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" /&gt;
              &lt;!-- property name="database" value="MYSQL" /--&gt;
          &lt;/bean&gt;    
          &lt;/property&gt;
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt;
                  &lt;prop key="javax.persistence.validation.mode"&gt;CALLBACK&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  &lt;bean id="dataSourceH2"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="org.h2.Driver" /&gt;
          &lt;property name="url" value="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1" /&gt;
          &lt;property name="username" value="sa" /&gt;
          &lt;property name="password" value="" /&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p><strong>RepositoryTest.java</strong></p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "/application-context-test.xml" })
  @Transactional
  public class SystemEntityRepositoryH2Test {
  
      @Inject
      private SystemEntityRepository repository;
  
      @Inject
      private DataSource dataSourceH2;
  
      private IDatabaseConnection connection;
  
      @Before
      public void setUp() throws Exception {
          IDatabaseConnection dbUnitCon = null;
          dbUnitCon = new DatabaseDataSourceConnection(dataSourceH2, "testdb");
          dbUnitCon.getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
  
          IDataSet dataSet = this.getDataSet("dataset-systementity.xml");
          DatabaseOperation.INSERT.execute(dbUnitCon, dataSet);
  
      }
  
      @After
      public void tearDown() throws Exception {
          //DatabaseOperation.DELETE_ALL.execute(this.getConnection(), this.getDataSet(dataSetFile));
      }
  
      @Test
      public void test() throws Exception {
  
      }
  
      protected IDataSet getDataSet(String dataSetFile) throws Exception {
          ResourceLoader resourceLoader = new ClassRelativeResourceLoader(this.getClass());
          Resource resource = resourceLoader.getResource(dataSetFile);
  
          if (resource.exists()) {
              return new FlatXmlDataSetBuilder().build(resource.getInputStream());
          }
  
          return null;
      }
  }
  </code></pre>
  
  <p><strong>dataset-systementity.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;dataset&gt;
      &lt;System_Entities id="2" name="NAME" phone01="+52-55-55555555" email="a@a.com"
                      address01="Street" address02="123" address03="1" address04="Address04"
                      address05="Address05" city="City" state="State" country="MX"
                      zipcode="12345" /&gt;
  &lt;/dataset&gt;
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <pre><code>ERROR DatabaseDataSet:286 - Table 'System_Entities' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[], _tableMap={}, _caseSensitiveTableNames=false]
  </code></pre>
  
  <p>I can see that the tables are created by hibernate because the log shows all the sql sentences without error.</p>
  
  <p>Thanks.</p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>Thanks Mark Robinson
  I modified the setUp method to:</p>
  
  <pre><code>@Before
      public void setUp() throws Exception {
          IDatabaseConnection dbUnitCon = null;
          EntityManager entityManager = entityManagerFactory.createEntityManager();
          Session session = entityManager.unwrap(Session.class);
          SessionImplementor si = (SessionImplementor) session;
          Connection conn = si.getJdbcConnectionAccess().obtainConnection();
          dbUnitCon = new DatabaseConnection(conn);
  
          //dbUnitCon.getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
  
          IDataSet dataSet = this.getDataSet("dataset-systementity.xml");
          DatabaseOperation.INSERT.execute(dbUnitCon, dataSet);
      }
  </code></pre>
  
  <p>It works now, what I don't understand yet is if I use HSQLDB I don't have this problem.</p>