【问题标题】:How to load data with data.sql using JHipster and Spring Boot如何使用 JHipster 和 Spring Boot 使用 data.sql 加载数据
【发布时间】:2017-12-29 05:16:08
【问题描述】:

我有一个 Jhipster 单片应用程序。我删除了 Liquibase,我想使用 data.sql 文件插入初始数据。我创建了一个包含插入脚本的 data.sql 和 data-h2.sql。它们位于src/main/resources 下。但似乎没有插入任何数据。

如何在启动过程中使用data.sql插入数据,而不使用Liquibase?

【问题讨论】:

  • 嗯,这不完全是我的问题的答案。我想使用 Spring (https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc) 初始化我的数据库。顺便说一句,作者引用了DataSourceInitializer 类,它帮助我找出了我的问题。

标签: spring-boot jhipster


【解决方案1】:

评论spring.datasource.type 属性并添加spring.jpa.hibernate.create-drop 属性有助于解决此问题。我认为一些 jhipster 配置会覆盖默认的 spring-boot 配置。

我是这样想出这个解决方案的:

在调试DataSourceInitializedPublisher.publishEventIfRequired 时,我看到以下行返回了null 引用:

private void publishEventIfRequired(EntityManagerFactory entityManagerFactory) {
    DataSource dataSource = findDataSource(entityManagerFactory);
    ...
}

我在application-dev.yml 中评论了spring.datasource.type 属性。这样spring就自动切换到tomcat连接池了。

然后,我发现DataSourceInitializedPublisher.isInitializingDatabase需要在配置中定义spring.jpa.hibernate.hbm2ddl.auto属性:

private boolean isInitializingDatabase(DataSource dataSource) {
    ...
    if (hibernate.containsKey("hibernate.hbm2ddl.auto")) {
        return true;
    }
    return false;
}

添加缺失的属性后,spring-boot开始执行data.sql文件。

更新

对于那些想要执行特定于平台的data.sql 文件,例如data-h2.sql,您还应该添加以下属性:

spring.datasource.platform=h2

更新

如果需要将存储在csv文件中的Jhipster默认数据转换为sql格式,可以参考以下要点:

https://gist.github.com/hkarakose/cf7f1b5b241dad611ba01c0211f42108

【讨论】:

    猜你喜欢
    • 2017-12-18
    • 2016-10-13
    • 2021-09-29
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 2020-09-29
    • 1970-01-01
    相关资源
    最近更新 更多