为什么Spring Boot 2.0应用程序不运行schema.sql? [英] Why Spring Boot 2.0 application does not run schema.sql?
问题描述
当我使用Spring Boot 1.5时,在应用程序启动时,Hibernate在设置了适当的配置时执行了位于 / resources 文件夹中的 schema.sql 文件。在Spring Boot 2.0发布之后,此功能不再起作用。我在文档中找不到任何有关此更改的内容。
这是我的 application.properties 文件内容:
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
Spring Boot 2.0是否有一些变化或这是一个错误/问题?
检查文件这里。
在基于JPA的应用程序中,您可以选择让Hibernate创建架构
或使用schema.sql,但是你不能两者兼得。如果你使用schema.sql,请务必禁用
spring.jpa.hibernate.ddl-auto。
你有 spring.jpa.hibernate.ddl-auto = create-drop
这就是为什么 schema.sql
未执行的原因。
看起来这是Spring Boot的工作方式。
编辑
<我认为问题(不是真正的问题)是你的应用程序指向一个mysql实例。
请参阅当前的Spring Boot属性:
spring.datasource.initialization-mode = embedded #使用可用的DDL和DML脚本初始化数据源。
默认值为 embedded
- 例如只有在你运行和嵌入式数据库时才会初始化,比如H2。
另请参阅Stephan的答案这里。他说:
将spring.datasource.initialization-mode = always添加到你的项目中足够
。
所以尝试设置:
spring.datasource .initialization-mode =总是
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?
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
这篇关于为什么Spring Boot 2.0应用程序不运行schema.sql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!