当我试图为我的Spring Boot应用程序创建内存中的H2 DB时,我被我得到的错误弄糊涂了。相关配置为
db.url=jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:create.sql'
hibernate.hbm2ddl.auto=create
和create.sql
CREATE TABLE `cities` (
`name` varchar(45) NOT NULL,
PRIMARY KEY (`name`)
) ;
INSERT INTO `cities` VALUES ('JAEN'),('ALBACETE');
但是我得到了错误Caused by: org.h2.jdbc.JdbcSQLException: Table "CITIES" already exists;
奇怪的是,如果我删除CREATE TABLE
语句,我会得到:
Caused by: org.h2.jdbc.JdbcSQLException: Table "CITIES" not found;
唯一有效的方法就是使用DROP TABLE IF EXISTS
,但我认为我不需要这样做。
到底怎么回事?将静态数据预先填充到H2内存DB中的正确方法是什么?
发布于 2016-06-03 23:46:20
1) Hibernate方式:使用import.sql文件或指定文件
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:使用默认的schema.sql & data.sql文件或通过属性指定文件
spring.datasource.schema = file1.sql spring.datasource.data = file1.sql, file2.sql
https://stackoverflow.com/questions/37616147
复制相似问题