在存在schema.sql

时间:2017-04-29 05:27:37

标签: java spring spring-boot spring-data-jpa

注意:我在Spring启动存储库上打开了一个问题。这是链接

https://github.com/spring-projects/spring-boot/issues/9048

我正在尝试在我的实体中插入一些行,以便使用H2数据库在开发人员计算机上进行测试。我正在使用data.sql

它工作正常,创建实体然后运行data.sql以在实体生成的表中插入数据。

但是我需要创建一些没有实体类的其他表,所以我正在使用schema.sql。这是问题,只要我将schema.sql添加到项目中,Spring Boot就会在创建实体之前运行data.sql,并以Table not found异常结束。

  

如何让data.sql使用schema.sql和实体   同时上课?

以下是该项目的示例代码。

用于重现问题的功能性maven项目的Git链接。

https://github.com/ConsciousObserver/SpringBootSchemaSqlIssue.git

package com.test;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

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

@SpringBootApplication
public class TestDataSqlApplication {

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

@Entity
@Table(name="USER_DETAILS")
class UserDetails {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

schema.sql文件

create table test(id int(10), name varchar(10));

data.sql

insert into USER_DETAILS VALUES(1, 'user1');
insert into USER_DETAILS VALUES(2, 'user2');
insert into USER_DETAILS VALUES(3, 'user3');

修改 *

根据@abagheldata.sql重命名为import.sql的建议,但import.sql无条件运行。那不是我需要的。

对于测试,我有一个maven配置文件可以激活特定的spring.datasource.platform = h2,从而迫使spring加载schema-h2.sqldata-h2.sql。不幸的是,平台对import.sql没有影响,因此将其重命名为import-h2.sql会阻止Spring加载它。

这是平台更改的分支,以重现此问题。

https://github.com/ConsciousObserver/SpringBootSchemaSqlIssue/tree/platform-h2

2 个答案:

答案 0 :(得分:1)

data.sql重命名为import.sql,您的应用程序将启动而不会出现任何错误。请参阅here上的相关文档。

答案 1 :(得分:0)

我遇到了同样的问题。我的项目中有 data.sqlschema.sql 文件。我的解决方案是在 schema.sql 文件中添加表创建 DDL,由 data.sql 文件中的命令使用。

相关问题