我想在我的Spring引導應用程序啓動後插入一些測試數據。我有以下配置。我注意到,在創建JPA實體之前執行sql腳本導致insert語句失敗。有沒有其他配置我在這裏失蹤?正如我的配置中指出的,我使用Spring Boot和H2和EclipseLink作爲JPA。Spring引導數據初始化
參考 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 /主/資源放置
日誌
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註釋標記 –
添加實體 – Srini
你是否嘗試過設置'spring.jpa.hibernate.ddl-auto:create'屬性? https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-jpa –