CrudRepository不从schema.sql读取数据 [英] CrudRepository not reading data from schema.sql

查看:95
本文介绍了CrudRepository不从schema.sql读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Setup: I have a spring-boot application with a simple @Entity Customer object and CustomerRepository. I want to pre-load the database with test-data described here in my other question so I created schema.sql and data.sql files to load the database.

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

Problem: The CrudRepository seems to be using a different database than the one created with schema.sql and data.sql. I have not explicitly defined a datasource anywhere because I'm hoping that spring-boot can default everything for me (i.e., did not define spring.datasource in application.properties), and even if I do it doesn't do anything.

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

我没有收到任何错误只需在存储库上调用findAll()时不返回任何内容。

I don't get any errors it just returns nothing when I invoke findAll() on the repository.

schema.sql

drop table customer if exists;

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

data.sql

data.sql

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

Customer.java

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

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

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>


推荐答案

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

I had the same problem and manage to fix it.

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

the problem wasn't the SQL files location (the log displayed the scripts execution).

这是使用像H2这样的内存数据库的DDL生成DDB的默认行为,以及在生成DDL之前执行SQL文件(默认情况下为内存数据库创建drop-drop)的事实。

It was the default behavior of DDL generation of Springboot with In-memory DB like H2 and the fact that the SQL files are executed before the DDL generation ('create-drop' by default for in-memory DB).

在application.properties中添加以下行:

add this line in application.properties :

spring.jpa.hibernate.ddl-auto=validate

,您就会很好。

这篇关于CrudRepository不从schema.sql读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆