Spring Boot中的JPA查询不会返回任何结果

时间:2018-04-22 21:40:02

标签: java spring-boot jpa h2

我使用H2数据库和一些JPA查询创建了一个非常小而简单的Spring Boot应用程序。

我的控制器如下:

package me.abhishek.springboot.microservice.example.todo.springbootmicroservicetodoservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ThingsToDoController {

    @Autowired
    ThingsToDoRepository repository;

    @GetMapping("/")
    public String index() {
        return "Hello from the ToDo Controller\n";
    }

    @GetMapping("/todo/{name}")
    public ThingsToDo getThingsToDo(@PathVariable String name) {
        ThingsToDo thingToDo=repository.findByNameIgnoreCaseContaining(name);

        return thingToDo;
    }
}

我的bean如下:

package me.abhishek.springboot.microservice.example.todo.springbootmicroservicetodoservice;

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

@Entity
@Table(name="things_to_do")
public class ThingsToDo {

    @Id
    private Long id;

    @Column(name="name")
    private String name;

    @Column(name="verified")
    private int verificationStatus;

    private String task;

    public ThingsToDo() {

    }

    public ThingsToDo(Long id, String name, int verificationStatus, String task) {
        super();
        this.id=id;
        this.name=name;
        this.verificationStatus=verificationStatus;
        this.task=task;
    }

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getVerificationStatus() {
        return verificationStatus;
    }

    public String getTask() {
        return task;
    }

    @Override
    public String toString() {
        return "ThingsToDo{" + 
                "id=" + id + 
                ", name='" + name + '\'' +
                ", verificationStatus='" + verificationStatus + '\'' +
                '}';
    }
}

H2数据库架构是:

create table things_to_do
(
    id int,
    name varchar(500),
    verified boolean
);

我为在DB中插入值而运行的查询是:

insert into things_to_do (id, name, verified) values (1, 'Hello', 1);
insert into things_to_do (id, name, verified) values (2, 'Bye', 0);

然而,当我到达http://localhost:8080/todo/Hello时,它并没有给我任何价值;如果我只是ping Hello from the ToDo Controller,它会输出http://localhost:8080。有人可以指出我错过了哪一步?谢谢!

编辑:从日志中(虽然我不太了解):

select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)

编辑:

ThingsToDoRepository如下:

package me.abhishek.springboot.microservice.example.todo.springbootmicroservicetodoservice;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ThingsToDoRepository extends JpaRepository<ThingsToDo, Long> {
    ThingsToDo findByNameIgnoreCaseContaining(String name);
}

1 个答案:

答案 0 :(得分:1)

尝试在spring.jpa.hibernate.ddl-auto文件中将属性update设置为application.properties

spring.jpa.hibernate.ddl-auto=update

由于H2是嵌入式数据库,因此Spring会自动将spring.jpa.hibernate.ddl-auto的默认值设置为create-drop,这会覆盖您的schema.sqldata.sql

  

Spring Boot会根据您的选择为您选择默认值   是否认为您的数据库是嵌入式的。它默认为   如果未检测到任何模式管理器,则为create-drop   其他情况。

reference

另外,您应在@Repository课程的顶部添加ThingsToDoRepository