Biggy_java
Biggy_java

Reputation: 465

Spring/Hibernate Integration Test with H2 DB

I am building a Spring/Hibernate/Postgres api, which works fine. I want to write an integration test using in memory H2 DB.I know that how to create test-applicationContext. But I am having few issues creating the tables.

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:processdb;INIT=RUNSCRIPT FROM 'classpath:create.sql'" 
        />
</bean>

right now the create.sql had a sql query to create the needed schema and tables. But Hiberate should take care of it and I think I do not have to use a query to create table, hibernate should take care of it from the model annotations? I have everything defined in my persistence.xml but at the end it says the table "user" cannot be found. Can any one suggest me how to create tables in integration test or point me in the right direction, Please? Thanks

Upvotes: 0

Views: 1837

Answers (1)

Martin Frey
Martin Frey

Reputation: 10075

You are missing the close_delay flag on the jdbc connection string. Without this H2 is closing the database each time the last connection is released. Thus the schema is lost is your case directly after creation.

jdbc:h2:mem:processdb;DB_CLOSE_DELAY=-1

Upvotes: 1

Related Questions