Paweł
Paweł

Reputation: 31

Spring Boot dont read data.sql but it read import.sql

I want import DML script after spring creates tables. I was fighting with data.sql file but my application don't see it. I don't know why. It works when I rename data.sql to import.sql, but it should also work with data.sql.

Anybody know why?

My application.properties:

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:postgresql://localhost:5432/yyy
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=org.postgresql.Driver

I put data.sql into src/main/resources

When only data.sql is in resources:

2018-03-21 00:42:13.646  INFO 4740 --- [           main] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@eebc0db'

When only import.sql (also in src/main/resources):

2018-03-21 00:48:57.023  INFO 16600 --- [           main] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'ScriptSourceInputFromUrl(file:/C:/Users/Pawel/Desktop/Project/target/classes/import.sql)'

When i type spring.datasource.data=data.sql into application.properties

Exception in thread "SimpleAsyncTaskExecutor-2" org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException: Property spring.datasource.data with value 'ServletContext resource [/data.sql]' is invalid: The specified resource does not exist.
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.getResources(DataSourceInitializer.java:169)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.getScripts(DataSourceInitializer.java:151)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.initSchema(DataSourceInitializer.java:114)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.onApplicationEvent(DataSourceInitializerInvoker.java:93)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.onApplicationEvent(DataSourceInitializerInvoker.java:37)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.lambda$multicastEvent$0(SimpleApplicationEventMulticaster.java:136)
    at java.lang.Thread.run(Thread.java:745)

I can see both data.sql and import.sql in target/classes/data.sql, target/classes/import.sql ...

Upvotes: 3

Views: 6169

Answers (4)

William Mendoza
William Mendoza

Reputation: 1

work for me when first option of naXa:

spring.datasource.data=classpath:/data.sql

Upvotes: 0

RoCaSo
RoCaSo

Reputation: 41

If somebody have this trouble, here is what I did:

applications.properties :

Oracle settings

spring.datasource.url = jdbc:oracle:thin:@localhost:1521:xe<br>
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver<br>
spring.datasource.username=xxxx<br>
spring.datasource.password=xxxx<br>
spring.jpa.hibernate.ddl-auto=none<br>
spring.datasource.initialization-mode=always<br>
spring.datasource.platform=oracle<br>

MYSQL settings

spring.datasource.url = jdbc:mysql://localhost:3306/mysql<br>
spring.datasource.username = xxxx<br>
spring.datasource.password = xxxx<br>
spring.jpa.hibernate.ddl-auto=none<br>
spring.datasource.initialization-mode=always<br>
spring.datasource.platform=mysql<br>

The lines that helped me was adding the last 2, and also remember that you must have a file with the name "data-oracle.sql" or "data-mysql.sql" respectively.

Here is the source:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc

Upvotes: 1

Gauthier Peel
Gauthier Peel

Reputation: 1518

you have to inactivate the Hibernate loading (from import.sql) by commenting your line spring.jpa.hibernate.ddl-auto=create-drop and setting it to validate. Then add spring.datasource.initialization:

spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=validate

Upvotes: 2

naXa stands with Ukraine
naXa stands with Ukraine

Reputation: 37916

If you put data.sql inside jar then prepend its name with classpath or META-INF

spring.datasource.data=classpath:/data.sql

spring.datasource.data=/META-INF/data.sql

(I'm not 100% sure so it would be great if you try both solution and give me feedback)

Upvotes: 2

Related Questions