如何使用JHipster和Spring Boot使用data.sql加载数据 [英] How to load data with data.sql using JHipster and Spring Boot

查看:92
本文介绍了如何使用JHipster和Spring Boot使用data.sql加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Jhipster monolithic application. I removed Liquibase and I want to use data.sql file to insert initial data. I created a data.sql and data-h2.sql which contain insert scripts. They are located under src/main/resources. But none of the data seems to be inserted.

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

How can I use data.sql to insert data during startup, without using Liquibase?

推荐答案

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

Commenting spring.datasource.type property and adding spring.jpa.hibernate.create-drop property helped resolve this issue. I think some jhipster configuration overrides default spring-boot configuration.

这是我提出此解决方案的方法:

Here is how I come up with this solution:

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

While debugging DataSourceInitializedPublisher.publishEventIfRequired, I saw that following line returned a null reference:

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

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

I commented spring.datasource.type property in application-dev.yml. In this way, spring switched to tomcat connection pool automatically.

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

Then, I found out that DataSourceInitializedPublisher.isInitializingDatabase required spring.jpa.hibernate.hbm2ddl.auto property to be defined in configuration:

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

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

After adding the missing property, spring-boot started to execute data.sql file.

更新

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

For those who want to execute a platform specific data.sql file, such as data-h2.sql, you should also add following property:

spring.datasource.platform=h2

更新

对于那些需要将存储在csv文件中的默认Jhipster数据转换为sql格式的人,您可以参考以下要点:

For those who need to convert default Jhipster data stored in csv files into sql format, you may refer to following gist:

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

这篇关于如何使用JHipster和Spring Boot使用data.sql加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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