Spring Boot 数据初始化 [英] Spring Boot Data Initialization

查看:37
本文介绍了Spring Boot 数据初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Spring Boot 应用程序启动后插入一些测试数据.我有以下配置.我确实注意到在创建 JPA 实体之前执行 sql 脚本导致插入语句失败.我在这里缺少任何其他配置吗?如我的配置中所述,我将 Spring Boot 与 H2 和 EclipseLink 用于 JPA.

I am trying to insert some test data after my Spring Boot application has started. I have the below configuration in place. I do notice that the sql script is executed before the JPA entities are created resulting in a failure of the insert statement. Is there any other configuration I am missing here? As noted in my configuration I am using Spring Boot with H2 and EclipseLink for 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

data.sql is placed under /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

推荐答案

这是 Spring 团队在此处确认的 Spring 中现有的错误.

This is an existing bug in Spring as confirmed here by the Spring Team.

https://github.com/spring-projects/spring-boot/issues/10365

这篇关于Spring Boot 数据初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆