繁体   English   中英

Spring Boot无法运行依赖于spring-cloud-starter-config的schema.sql

[英]Spring Boot fails to run schema.sql with dependency on spring-cloud-starter-config

我的spring boot 2.0应用程序识别并运行schema.sql来初始化我的嵌入式h2数据库。 但是,当我添加spring-cloud-starter-config依赖项时,该应用程序不再运行schema.sql。 为了说明这一点,请使用spring initializr生成依赖于以下内容的Spring Boot 2(v2.0.1)应用程序:

  • 网络
  • 休息库
  • pa
  • h2
  • 配置客户端

添加实体:

package com.example.demo;

import javax.persistence.*;

@Entity
public class Room {
    @Id
    @Column(name = "ROOM_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column
    private String name;

//...getters and setters

}

实体的存储库:

package com.example.demo;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RoomRepository extends CrudRepository<Room, Long> {

}

主类与initializr生成的内容保持不变:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

将schema.sql和data.sql文件添加到资源下的基本文件夹中。 Spring Boot使用这些来创建和填充实体的基础表:

schema.sql:

CREATE TABLE ROOM(
    ROOM_ID BIGINT AUTO_INCREMENT PRIMARY KEY,
    NAME VARCHAR(16) NOT NULL,
);

data.sql:

INSERT INTO ROOM (NAME) VALUES ('Piccadilly');
INSERT INTO ROOM (NAME) VALUES ('Cambridge');
INSERT INTO ROOM (NAME) VALUES ('Oxford');
INSERT INTO ROOM (NAME) VALUES ('Manchester');

将空的application.properties替换为此application.yml:

spring:
  jpa:
    hibernate.ddl-auto: none

现在,运行该应用程序并转到http:// localhost:8080 / rooms 应用程序将失败,并显示表不存在的JdbcSQLException:

org.h2.jdbc.JdbcSQLException:未找到表“ ROOM”; SQL语句:从room room0_中选择room0_.room_id作为room_id1_0_,将room0_.name作为name2_0_

现在进入pom.xml并注释掉对spring-cloud-starter-config依赖项的引用:

<!--
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
-->

重新启动应用程序,一切正常! 在浏览器中打开http:// localhost:8080 / rooms ,您将发现返回了预期的JSON结果(带有4行)。 这告诉我,是Spring-cloud-starter-config依赖项阻止了Spring执行schema.sql和data.sql初始化数据库。

使用Spring Cloud依赖项时如何获取Spring Boot以执行schema.sql和data.sql文件?

我有类似的问题,但是在执行H2数据库架构创建的测试期间。 我刚刚尝试了较新的版本spring-boot-starter-parent:2.0.2.RELEASE Cloud的spring-boot-starter-parent:2.0.2.RELEASEFinchley.RC2 ,它在我的情况下可以工作。

您的示例花了几分钟-在Spring Boot 2.0.1和Spring Cloud Finchley.RC1中可以重现,但在2.0.2和Finchley.RC2中可以正常工作。 我没有找到github问题,但是看起来像是固定的。

暂无
暂无

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

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