Rob
Rob

Reputation: 718

import initial data into spring boot app

According to the documentation http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html to import data on startup you can define these 2 properties inside application.properties:

spring.datasource.data=data.sql
spring.datasource.schema=schema.sql

This works when you have both of these files, but i don't have schema. I am generating db schema using hibernate

spring.jpa.hibernate.ddl-auto=create-drop

Previous workaround Importing Data with spring boot was to define file for schema.sql blank, but it doesn't work anymore. I will get exception that file can't be blank or null.

In spring application i simply defined

<jdbc:initialize-database data-source="simpleDataSource">
    <jdbc:script location="classpath:dbscripts/data.sql"/>
</jdbc:initialize-database>

and it worked.

Is there a way to have same behaviour also in springBoot?

Upvotes: 2

Views: 4533

Answers (2)

uanian
uanian

Reputation: 21

According to spring source code - data.sql will be invoked only if schema.sql file exist and it is not empty. So if you only need scripts from data.sql add some simple query to schema.sql like

SELECT 1;

Also in application.properties add

spring.datasource.initialization-mode=always

because default value is used only for embedded data sources.

Upvotes: 2

luboskrnac
luboskrnac

Reputation: 24561

Just delete schema.sql + config options and Spring Boot should automatically pick up only sata.sql.

Upvotes: 2

Related Questions