1

I'm confused by the errors I get when trying to create an in-memory H2 DB for my Spring Boot application. The relevant configuration is

db.url=jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:create.sql' hibernate.hbm2ddl.auto=create 

And create.sql:

CREATE TABLE `cities` ( `name` varchar(45) NOT NULL, PRIMARY KEY (`name`) ) ; INSERT INTO `cities` VALUES ('JAEN'),('ALBACETE'); 

But I get the error Caused by: org.h2.jdbc.JdbcSQLException: Table "CITIES" already exists;

Weird is, if I remove the CREATE TABLE statement, I get:

Caused by: org.h2.jdbc.JdbcSQLException: Table "CITIES" not found; 

The only thing that works is using DROP TABLE IF EXISTS, but well, I don't think I should need to.

What's going on? What's the proper way of pre-populating static data into an H2 memory DB?

1 Answer 1

1

1) Hibernate way: use import.sql file or specify files

spring.jpa.properties.hibernate.hbm2ddl.import_files=file1.sql,file2.sql

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

2) Spring Boot: use default schema.sql & data.sql files or specify files through properties

spring.datasource.schema = file1.sql spring.datasource.data = file1.sql, file2.sql

http://docs.spring.io/autorepo/docs/spring-boot/1.0.2.RELEASE/reference/html/howto-database-initialization.html

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.