bestsource

Spring Boot 2.0 응용 프로그램이 schema.sql을 실행하지 않는 이유는 무엇입니까?

bestsource 2023. 2. 9. 22:01
반응형

Spring Boot 2.0 응용 프로그램이 schema.sql을 실행하지 않는 이유는 무엇입니까?

Spring Boot 1.5를 사용하고 있을 때 어플리케이션 부팅 시 /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는 실행되지 않습니다.스프링 부츠가 이렇게 작동하는 것 같네요.

편집

문제는 (실제로 문제가 되지 않는) 어플리케이션이 mysql 인스턴스를 가리키는 것이라고 생각합니다.

현재 Spring Boot 속성을 참조하십시오.

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

기본값은 다음과 같습니다.embedded- 예를 들어 H2와 같은 내장형 데이터베이스를 실행하고 있는 경우에만 초기화합니다.

여기 스테판의 대답도 보세요.그는 말했다:

spring.datasource를 추가합니다.initialization-mode=프로젝트에는 항상 충분합니다.

설정해 보겠습니다.

spring.datasource.initialization-mode=always

내장되어 있지 않다(MySQL 등)

내장되지 않은 데이터베이스를 로드하는 경우 Spring Boot 2에서 다음을 추가해야 합니다.

spring.datasource.initialization-mode=always

이행가이드를 참조해 주세요.

데이터베이스 초기화

기본 DataSource 초기화는 임베디드 데이터 소스에 대해서만 활성화되며 프로덕션 데이터베이스를 사용하는 즉시 꺼집니다.새로운spring.datasource.initialization-mode(비활성화)spring.datasource.initialize)를 사용하면 컨트롤이 강화됩니다.


내장형(예: h2)

이전에 비슷한 문제가 있었습니다만, H2( 임베디드 DB)였는데, H2 구성이 활성화되었습니다.my-test프로파일링 합니다.

내 시험 수업은 다음과 같았다.

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

문제는요.@SpringBootTest테스트 데이터베이스를 초기화하지 않았습니다.난 그걸 써야 했어@DataJpaTest또는@SpringBootTest+@AutoConfigureTestDatabase.예

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

또는

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

최신 업데이트

Spring Boot 버전 2.7 현재

재산spring.datasource.initialization-mode가 삭제되었습니다.

이 버전 이상에서는 교체 속성을 사용해야 합니다.spring.sql.init.mode

예:spring.sql.init.mode:always

스프링 부츠 2.7 체인지로그

난 괜찮아, 한번 해봐.HikariCP 대신 원하는 데이터 소스 유형을 설정합니다.

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

sql을 설정하지.spring.jpa.hibernate.ddl-auto=nonedsql 실행되지 .

그렇게 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>

문제를 봐주세요.

언급URL : https://stackoverflow.com/questions/49438517/why-spring-boot-2-0-application-does-not-run-schema-sql

반응형