【问题标题】:CrudRepository not reading data from schema.sqlCrudRepository 未从 schema.sql 读取数据
【发布时间】:2015-05-09 09:33:22
【问题描述】:

设置:我有一个带有简单@Entity Customer 对象和 CustomerRepository 的 spring-boot 应用程序。我想用here in my other question 描述的测试数据预加载数据库,所以我创建了 schema.sql 和 data.sql 文件来加载数据库。

问题: CrudRepository 使用的数据库似乎与使用 schema.sql 和 data.sql 创建的数据库不同。我没有在任何地方明确定义数据源,因为我希望 spring-boot 可以为我默认所有内容(即,没有在 application.properties 中定义 spring.datasource),即使我这样做也不会做任何事情。

@Autowired
CustomerRepository r;
r.findAll(); // nothing but it should return the row "John Doe"

我没有收到任何错误,当我在存储库上调用 findAll() 时它什么也不返回。

schema.sql

drop table customer if exists;

create table customer (
    id bigint auto_increment,
    firstname varchar(80) null,
    lastname varchar(80) null
);

数据.sql

insert into customer (firstname, lastname) values ('John', 'Doe');

客户.java

package sample.jsp;

import java.io.Serializable;

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

@Entity
public class Customer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    private String firstName;
    private String lastName;

    protected Customer() {}

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public long getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }

}

CustomerRepository.java

package sample.jsp;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);
    List<Customer> findAll();
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.8.RELEASE</version>
    </parent>
    <artifactId>TestApp</artifactId>
    <packaging>war</packaging>
    <name>Spring Boot Web JSP Sample</name>
    <description>Spring Boot Web JSP Sample</description>
    <url>http://projects.spring.io/spring-boot/</url>
    <organization>
        <name>Pivotal Software, Inc.</name>
        <url>http://www.spring.io</url>
    </organization>
    <properties>
        <main.basedir>${basedir}/../..</main.basedir>
        <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>           
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • 谁知道这些“sql”数据文件?什么进程应该执行它们?春天? JPA2.1 电动势初始化?

标签: spring hibernate jpa spring-boot crud


【解决方案1】:

data.sql 的名称更改为 import.sql

Spring Boot Database Initialization

【讨论】:

  • 我创建了一个带有内存 H2 数据库的 spring boot + kotlin 沙箱,并希望用测试数据填充它。我遇到了类似的问题 - 我的 schema.sql 文件运行但 data.sql 没有。将其从 data 重命名为 import.sql 解决了我的问题。谢谢!
【解决方案2】:

我遇到了同样的问题并设法解决它。

问题不在于 SQL 文件位置(日志显示脚本执行)。

It was the default behavior of DDL generation of Springboot with In-memory DB like H2 以及 SQL 文件在 DDL 生成之前执行的事实(内存数据库默认为“create-drop”)。

在 application.properties 中添加这一行:

spring.jpa.hibernate.ddl-auto=validate

你会很好的。

【讨论】:

  • 我面临着类似的问题。出于测试目的,我将数据从我的测试用例插入到内存数据库中。在使用我的测试用例方法中的存储库 findAll 方法查询数据库时,它会获取插入的数据,但是当我的集成测试运行并且我的服务层尝试使用 repository.findAll 获取数据时,它会返回一个空集。对这种奇怪的行为有任何想法吗?
【解决方案3】:

我遇到了类似的问题并成功解决了。

为了使它工作,我必须为我需要测试数据的每个测试方法添加这个注释:

@Sql("classpath:data.sql")

我正在使用带有 H2 的 spring-boot-starter-data-jpa。就我而言,我运行了一个依赖于一些初始数据(在data.sql 创建)的测试。

当我运行测试时,架构已正确创建,但与您的情况一样,CrudRepository 似乎使用了不同的数据。

注意:我知道回答这个问题可能会迟到,但我希望这对遇到同样问题的人有所帮助。

【讨论】:

    【解决方案4】:

    Spring Boot 文档的这一部分描述了它是如何工作的:

    http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#howto-intialize-a-database-using-spring-jdbc

    特别是你必须确保两个 sql 文件都在类路径的根目录中。或者,您可以在application.properties 中指定(甚至只是为了看看它是否以这种方式工作)确切的位置/文件名。

    spring.datasource.schema=file:scripts/my-schema.sql
    

    【讨论】:

      猜你喜欢
      • 2019-07-06
      • 1970-01-01
      • 2018-03-30
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多