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.
How can I use data.sql to insert data during startup, without using Liquibase?
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:
While debugging DataSourceInitializedPublisher.publishEventIfRequired
, I saw that following line returned a null
reference:
private void publishEventIfRequired(EntityManagerFactory entityManagerFactory) {
DataSource dataSource = findDataSource(entityManagerFactory);
...
}
I commented spring.datasource.type
property in application-dev.yml
. In this way, spring switched to tomcat connection pool automatically.
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;
}
After adding the missing property, spring-boot started to execute data.sql
file.
Update
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
Update
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