【发布时间】:2018-02-14 10:25:40
【问题描述】:
我试图在我的 Spring Boot 应用程序启动后插入一些测试数据。我有以下配置。我确实注意到在创建 JPA 实体之前执行了 sql 脚本,导致插入语句失败。我在这里还缺少其他配置吗?如我的配置中所述,我将 Spring Boot 与 H2 和 EclipseLink 用于 JPA。
参考 https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html
配置
# H2
spring.h2.console.enabled=true
# Datasource
spring.datasource.url=jdbc:h2:mem:testdb;MODE=ORACLE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
# JPA
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.properties.eclipselink.weaving=false
data.sql 放在 /src/main/resources 下
日志
2017-09-05 20:37:37,989 [restartedMain ] INFO
o.s.j.d.e.EmbeddedDatabaseFactory - Starting embedded database:
url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false',
username='sa'
2017-09-05 20:37:38,141 [restartedMain ] INFO
j.LocalContainerEntityManagerFactoryBean - Building JPA container
EntityManagerFactory for persistence unit 'default'
2017-09-05 20:37:38,446 [restartedMain ] INFO
o.s.o.j.AbstractEntityManagerFactoryBean - Initialized JPA
EntityManagerFactory for persistence unit 'default'
2017-09-05 20:37:38,510 [restartedMain ] INFO o.s.j.d.i.ScriptUtils
- Executing SQL script from URL [file:/develop/src/data-init/target/classes/data.sql]
error...
实体
@Entity
@Table(name = "TEMPLATE_CONFIG")
@Data
public class TemplateUser {
@Id
@Column(name = "TEMPLATE_CONFIG_ID", nullable = false)
private Long configId;
@Column(name = "APP_CODE", nullable = false, length = 100)
private String appCode;
...
}
更新
上传了一个示例项目: https://git.io/v5SWx
【问题讨论】:
-
你能发布你的实体的样子吗?您需要用@Entity 注释标记它们
-
添加了实体
-
您是否也尝试过设置
spring.jpa.hibernate.ddl-auto: create属性? docs.spring.io/spring-boot/docs/current/reference/html/… -
如果我没有使用 Hibernate,这有什么问题吗?
-
您可以尝试将数据源 url 更改为类似 jdbc:h2:mem:testdb?createDatabaseIfNotExist=true
标签: spring spring-boot