简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM