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

后端 未结 5 2048
抹茶落季
抹茶落季 2020-12-09 01:54

While I was using Spring Boot 1.5, on application startup Hibernate executed schema.sql file located in /resources folder when appropriate configuration is

相关标签:
5条回答
  • 2020-12-09 02:23

    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
    
    0 讨论(0)
  • 2020-12-09 02:28

    Not embedded (e.g. MySQL)

    If you load a database that is not embedded, in Spring Boot 2 you need to add:

    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.


    Embedded (e.g. 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.

    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. I had to either use @DataJpaTest or @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 {
    
    0 讨论(0)
  • 2020-12-09 02:29

    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.

    0 讨论(0)
  • 2020-12-09 02:32

    It works fine for me, you can try it. Set datasource type to what you like instead of HikariCP.

    spring.datasource.initialization-mode=always
    spring.datasource.type=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
    spring.jpa.hibernate.ddl-auto=none
    
    0 讨论(0)
  • 2020-12-09 02:34

    I was able to make application run only after excluding Hikary CP like that:

    <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

    0 讨论(0)
提交回复
热议问题