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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49438517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 03:01:02  来源:igfitidea点击:

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

javaspringhibernatespring-boot

提问by ?or?e Pr?ulj

While I was using Spring Boot 1.5, on application startup Hibernate executed schema.sqlfile located in /resourcesfolder 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.propertiesfile content:

当我使用 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

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

Spring Boot 2.0 是否有一些变化,或者这是一个错误/问题?

采纳答案by Evgeni Dimitrov

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.

在基于 JPA 的应用程序中,您可以选择让 Hibernate 创建架构或使用 schema.sql,但您不能两者兼而有之。如果您使用 schema.sql,请确保禁用 spring.jpa.hibernate.ddl-auto。

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

你有spring.jpa.hibernate.ddl-auto=create-drop这就是为什么schema.sql没有被执行。看起来这就是 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- e.g. initialize only if you're running and embedded database, like H2.

默认值是embedded- 例如,仅当您正在运行和嵌入式数据库(如 H2)时才初始化。

Also see the answer of Stephan here. He said:

也可以在这里看到 Stephan 的回答。他说:

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

回答by acdcjunior

Not embedded (e.g. 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 (e.g. h2)

嵌入式(例如 h2)

I once had a similar problem, even though it was an h2 (so it wasan embedded DB), my h2 configuration was activated by a my-testprofile.

我曾经遇到过类似的问题,即使它是一个 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 @SpringBootTestalone did not initialize the test database. I had to either use @DataJpaTestor @SpringBootTest+@AutoConfigureTestDatabase. Examples

问题是@SpringBootTest单独没有初始化测试数据库。我不得不使用@DataJpaTest@SpringBootTest+ @AutoConfigureTestDatabase。例子

@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 {

回答by Hyman Zhou

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

回答by jay chang

There have another problem may result data.sql can not be executed,when you don't config the spring.jpa.hibernate.ddl-auto=none,that the data.sql will not be execute d.

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

回答by stinger

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

请在这里查看问题