首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弹簧启动数据初始化

弹簧启动数据初始化
EN

Stack Overflow用户
提问于 2017-09-06 03:58:17
回答 2查看 2.2K关注 0票数 0

我正在尝试在我的Spring应用程序启动后插入一些测试数据。我有下面的配置到位。我确实注意到,sql脚本是在创建JPA实体之前执行的,从而导致insert语句失败。这里还有其他的配置吗?正如在我的配置中所指出的,我使用Spring和H2和EclipseLink来实现JPA。

Reference https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

配置

代码语言:javascript
复制
# 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/下面。

日志

代码语言:javascript
复制
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...

实体

代码语言:javascript
复制
@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

EN

回答 2

Stack Overflow用户

发布于 2017-09-13 18:46:04

data.sql或data-${somthing}.sql

你可以:

  • spring.datasource.platform=h2。
  • =myscript-test.sql。(可以用此属性更改位置)

我有一个这样的项目,并且正在与h2合作。

还有最后一个选择,如果您无法加载这些信息,也许您可以在应用程序文件中使用jdbc模板使用CommandLineRunner。

票数 0
EN

Stack Overflow用户

发布于 2017-09-21 04:57:27

这是Spring中存在的一个bug,Spring团队在这里证实了这一点。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46066575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档