DbUnit H2 in memory db with Spring and Hibernate -


hi i'm trying little poc jpa , unit test verify db schema created. i'm working h2 db , set hibernate create schema entities, when dbunit tries initialize db dataset table ... not found in tablemap. read have add property db_close_delay=-1 db url after hibernate creates schema db losted when dbunit tries initialize.

any ideas? highly appreciated.

this config:

application-context.xml

<bean id="entitymanagerfactory"     class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean">     <property name="datasource" ref="datasourceh2" />     <property name="packagestoscan" value="com.xxx.model" />     <property name="jpavendoradapter">         <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter">             <property name="generateddl" value="true" />             <property name="showsql" value="true" />             <!-- property name="databaseplatform" value="org.hibernate.dialect.mysqlinnodbdialect" /-->             <property name="databaseplatform" value="org.hibernate.dialect.h2dialect" />             <!-- property name="database" value="mysql" /-->         </bean>             </property>         <property name="jpaproperties">             <props>                 <prop key="hibernate.hbm2ddl.auto">create</prop>                 <prop key="javax.persistence.validation.mode">callback</prop>             </props>         </property>     </bean>  <bean id="datasourceh2"         class="org.springframework.jdbc.datasource.drivermanagerdatasource">         <property name="driverclassname" value="org.h2.driver" />         <property name="url" value="jdbc:h2:mem:testdb;db_close_delay=-1" />         <property name="username" value="sa" />         <property name="password" value="" />     </bean> 

repositorytest.java

@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;     } } 

dataset-systementity.xml

<?xml version="1.0" encoding="utf-8"?> <dataset>     <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" /> </dataset> 

error

error databasedataset:286 - table 'system_entities' not found in tablemap=org.dbunit.dataset.orderedtablenamemap[_tablenames=[], _tablemap={}, _casesensitivetablenames=false] 

i can see tables created hibernate because log shows sql sentences without error.

thanks.

solution

thanks mark robinson modified setup method to:

@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);     } 

it works now, don't understand yet if use hsqldb don't have problem.

the problem dbunit loading table data before hibernate can initialize.

as part of @setup, you'll need hibernate session. should cause hibernate create table. force executing simple query select 1


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -