【问题标题】:JPA query in Spring Boot does not return any resultSpring Boot中的JPA查询不返回任何结果
【发布时间】:2018-10-02 21:42:13
【问题描述】:

我使用 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;
    }
}

我的豆子如下:

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

我运行以将值插入数据库的查询是:

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 http://localhost:8080,它会输出 Hello from the ToDo Controller。有人可以指出我缺少哪一步吗?谢谢!

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

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

【问题讨论】:

  • @TwiN,通过在线访问控制台。好吧,我确实有 schema.sqldata.sql 但由于某种原因它们没有运行(我猜)。
  • @TwiN,我没有尝试过,因为无论如何我都无法得到任何结果!
  • @TwiN,您能否确认我的查询方式是否正确?我不确定何时调用构造函数以及该构造函数是否会按预期工作(因为我在表中没有 task 列)。
  • @TwiN,没关系。谢谢你。 :)

标签: java spring-boot jpa h2


【解决方案1】:

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

spring.jpa.hibernate.ddl-auto=update

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

Spring Boot 会根据你选择一个默认值 它是否认为您的数据库是嵌入式的。它默认为 如果没有检测到模式管理器或根本没有检测到模式管理器,则创建删除 其他情况。

reference

此外,您应该在 ThingsToDoRepository 类的顶部添加 @Repository

【讨论】:

  • 感谢您的所有帮助。您对代码的审查对我帮助很大。谢谢! :)
猜你喜欢
  • 2017-08-29
  • 1970-01-01
  • 2020-08-13
  • 2021-11-23
  • 2016-07-01
  • 2021-05-25
  • 2019-03-01
  • 1970-01-01
相关资源
最近更新 更多