1

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?

Halil
  • 1,795
  • 1
  • 24
  • 39
  • Looks like a duplicate of https://stackoverflow.com/questions/24508223/multiple-sql-import-files-in-spring-boot – Gaël Marziou Jul 23 '17 at 17:35
  • Well, that isn't exactly the answer to my question. I want to initialize my database using Spring (`https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc`). By the way, the author references `DataSourceInitializer` class which helped me find out my problem. – Halil Jul 23 '17 at 20:07

1 Answers1

0

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

Halil
  • 1,795
  • 1
  • 24
  • 39