CrudRepository没有从schema.sql读取数据

时间:2015-03-07 23:51:35

标签: spring hibernate jpa spring-boot crud

设置:我有一个带有简单的@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),即使我这样做也没有做任何事情。 / p>

@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
);

data.sql

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

Customer.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>

4 个答案:

答案 0 :(得分:3)

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

Spring Boot Database Initialization

答案 1 :(得分:2)

我遇到了同样的问题并设法修复它。

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

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

在application.properties中添加此行:

spring.jpa.hibernate.ddl-auto=validate

你会很好。

答案 2 :(得分:2)

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

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

@Sql("classpath:data.sql")

我正在使用spring-boot-starter-data-jpa和H2。就我而言,我运行的测试取决于一些初始数据(在data.sql中创建)。

当我运行测试时,架构是正确创建的,但就像你的情况一样,CrudRepository似乎使用了不同的数据。)

注意: 我知道回答这个问题可能会迟到,但我希望这可以帮助那些遇到同样问题的人。 < / p>

答案 3 :(得分:0)

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