Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Spring Boot 2.0 application does not run schema.sql?

While I was using Spring Boot 1.5, on application startup Hibernate executed schema.sql file located in /resources folder when appropriate configuration is set. After Spring Boot 2.0 release this feature does not work any more. I couldn't find anything about this change in documentation. Here is my application.properties file content:

spring.datasource.url=... spring.datasource.username=... spring.datasource.password=...  #spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.hibernate.ddl-auto=none spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 

Is there some change in Spring Boot 2.0 or is this an bug/issue?

like image 487
Đorđe Pržulj Avatar asked Mar 22 '18 21:03

Đorđe Pržulj


People also ask

Where do I put schema in spring boot SQL?

Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema. sql and data. sql , respectively.

Does spring boot use SQL?

Spring Data includes repository support for JDBC and will automatically generate SQL for the methods on CrudRepository . For more advanced queries, a @Query annotation is provided. Spring Boot will auto-configure Spring Data's JDBC repositories when the necessary dependencies are on the classpath.


1 Answers

Check the documents here.

In a JPA-based app, you can choose to let Hibernate create the schema or use schema.sql, but you cannot do both. Make sure to disable spring.jpa.hibernate.ddl-auto if you use schema.sql.

You have spring.jpa.hibernate.ddl-auto=create-drop that's why schema.sql is not executed. Looks like this is the way Spring Boot works.

Edit

I think that the problem(not really a problem) is that your application points to a mysql instance.

See the current Spring Boot properties:

spring.datasource.initialization-mode=embedded # Initialize the datasource with available DDL and DML scripts. 

The default value is embedded - e.g. initialize only if you're running and embedded database, like H2.

Also see the answer of Stephan here. He said:

Adding spring.datasource.initialization-mode=always to your project is enough.

So try to set:

spring.datasource.initialization-mode=always 
like image 169
Evgeni Dimitrov Avatar answered Sep 24 '22 03:09

Evgeni Dimitrov