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

后端 未结 5 2076
抹茶落季
抹茶落季 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: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 {
    

提交回复
热议问题