我正在尝试在我的Spring应用程序启动后插入一些测试数据。我有下面的配置到位。我确实注意到,sql脚本是在创建JPA实体之前执行的,从而导致insert语句失败。这里还有其他的配置吗?正如在我的配置中所指出的,我使用Spring和H2和EclipseLink来实现JPA。
Reference 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=falsedata.sql放在/src/main/下面。
日志
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
发布于 2017-09-13 18:46:04
data.sql或data-${somthing}.sql
你可以:
我有一个这样的项目,并且正在与h2合作。
还有最后一个选择,如果您无法加载这些信息,也许您可以在应用程序文件中使用jdbc模板使用CommandLineRunner。
发布于 2017-09-21 04:57:27
这是Spring中存在的一个bug,Spring团队在这里证实了这一点。
https://stackoverflow.com/questions/46066575
复制相似问题