简体   繁体   English

为什么 Spring Boot 2.0 应用程序不运行 schema.sql?

[英]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.当我使用 Spring Boot 1.5 时,在应用程序启动时,Hibernate 在设置了适当的配置后执行了位于/resources文件夹中的schema.sql文件。 After Spring Boot 2.0 release this feature does not work any more.在 Spring Boot 2.0 发布后,此功能不再起作用。 I couldn't find anything about this change in documentation.我在文档中找不到有关此更改的任何信息。 Here is my application.properties file content:这是我的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

Is there some change in Spring Boot 2.0 or is this an bug/issue? Spring Boot 2.0 是否有一些变化,或者这是一个错误/问题?

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.在基于 JPA 的应用程序中,您可以选择让 Hibernate 创建模式或使用 schema.sql,但不能同时使用这两种方法。 Make sure to disable spring.jpa.hibernate.ddl-auto if you use schema.sql.如果您使用 schema.sql,请确保禁用 spring.jpa.hibernate.ddl-auto。

You have spring.jpa.hibernate.ddl-auto=create-drop that's why schema.sql is not executed.你有spring.jpa.hibernate.ddl-auto=create-drop这就是为什么schema.sql没有被执行。 Looks like this is the way Spring Boot works.看起来这就是 Spring Boot 的工作方式。

Edit编辑

I think that the problem(not really a problem) is that your application points to a mysql instance.我认为问题(不是真正的问题)是您的应用程序指向一个 mysql 实例。

See the current Spring Boot properties :查看当前的 Spring Boot 属性

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

The default value is embedded - eg initialize only if you're running and embedded database, like H2.默认值是embedded的 - 例如,仅当您正在运行和嵌入数据库(如 H2)时才初始化。

Also see the answer of Stephan here .另请参阅 Stephan答案。 He said:他说:

Adding spring.datasource.initialization-mode=always to your project is enough.将 spring.datasource.initialization-mode=always 添加到您的项目中就足够了。

So try to set:所以尝试设置:

spring.datasource.initialization-mode=always

Not embedded (eg MySQL)未嵌入(例如 MySQL)

If you load a database that is not embedded , in Spring Boot 2 you need to add:如果你加载了一个未嵌入的数据库,在 Spring Boot 2 中你需要添加:

spring.datasource.initialization-mode=always

Check the Migration Guide :检查迁移指南

Database Initialization数据库初始化

Basic DataSource initialization is now only enabled for embedded data sources and will switch off as soon as you're using a production database.基本数据源初始化现在仅对嵌入式数据源启用,并且会在您使用生产数据库时立即关闭。 The new spring.datasource.initialization-mode (replacing spring.datasource.initialize ) offers more control.新的spring.datasource.initialization-mode (替换spring.datasource.initialize )提供了更多控制。


Embedded (eg h2)嵌入式(例如 h2)

I once had a similar problem, even though it was an h2 (so it was an embedded DB), my h2 configuration was activated by a my-test profile.我曾经遇到过类似的问题,即使它是一个 h2(所以它一个嵌入式数据库),我的 h2 配置是由一个my-test配置文件激活的。

My test class was like:我的测试课是这样的:

@RunWith(SpringRunner.class)
@SpringBootTest                     // does not work alone
@ActiveProfiles("my-test")
public class MyEntityRepositoryTest {

The problem is @SpringBootTest alone did not initialize the test database .问题是@SpringBootTest单独没有初始化测试数据库 I had to either use @DataJpaTest or @SpringBootTest + @AutoConfigureTestDatabase .我不得不使用@DataJpaTest@SpringBootTest + @AutoConfigureTestDatabase Examples例子

@RunWith(SpringRunner.class)
@DataJpaTest                       // works
@ActiveProfiles("sep-test")
public class MyEntityRepositoryTest {

or或者

@RunWith(SpringRunner.class)
@SpringBootTest                     // these two
@AutoConfigureTestDatabase          // together work
@ActiveProfiles("sep-test")
public class MyEntityRepositoryTest {

It works fine for me, you can try it.对我来说很好用,你可以试试。 Set datasource type to what you like instead of HikariCP.将数据源类型设置为您喜欢的类型,而不是 HikariCP。

spring.datasource.initialization-mode=always
spring.datasource.type=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
spring.jpa.hibernate.ddl-auto=none

Recent Update最近更新

As of Spring Boot Version 2.7从 Spring Boot 2.7 版开始

the property spring.datasource.initialization-mode has been removed.属性spring.datasource.initialization-mode已被删除。

You should from this version and onwards use the replacement property spring.sql.init.mode你应该从这个版本开始使用替换属性spring.sql.init.mode

Example: spring.sql.init.mode:always示例: spring.sql.init.mode:always

Spring Boot 2.7 changelog Spring Boot 2.7 更新日志

还有一个问题可能导致data.sql无法执行,当你不配置spring.jpa.hibernate.ddl-auto=none时,data.sql不会被执行

I was able to make application run only after excluding Hikary CP like that:只有在像这样排除 Hikary CP 之后,我才能使应用程序运行:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Please, see the issue here请在这里查看问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM