【问题标题】:Query Creation Spring Data JPA查询创建 Spring Data JPA
【发布时间】:2019-09-03 16:55:29
【问题描述】:

我有一堆汽车对象。我能够呈现所有汽车对象的列表,每个字段都显示在list.html 上。如果我只想在list.html 上显示make 值的列表,我将如何使用查询来解决这个问题?查看 Spring Data JPA Docs 第 5.3.2 节,我可以做类似findByAllMake(); 的事情吗?

我试过findByAllMake();...

以下代码显示了我如何成功获取所有对象的完整列表。只想获取所有makes 的列表。

汽车.java

package com.example.demo;


import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Car {

    @Id
    private String id;

    private String make;
    private String model;
    private String year;


    public Car() {
    }

    public Car(String id, String make, String model, String year) {
        this.id = id;
        this.make = make;
        this.model = model;
        this.year = year;
    }

    public String getId() {
        return id;
    }

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

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }
}

汽车控制器.java:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;


@Controller
public class CarController {



    @Autowired
    private CarRepository carRepository;


    @GetMapping("/list")
    public String carMakeList(Model model){
        model.addAttribute("list", carRepository.getAllMakes());

        return "list";
    }


}

CarRepository.java

package com.example.demo;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CarRepository extends JpaRepository<Car, String> {
    @Query(value="SELECT * FROM car;", nativeQuery=true)
    List<Car> getAllMakes();


}

我已经尝试过findByAllMake();,但我得到了一个 Unsatisfactory Dependency 错误。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carController': Unsatisfied dependency expressed through field 'carService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carService': Unsatisfied dependency expressed through field 'carRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.demo.CarRepository.findAllByMake()! No parameter available for part make SIMPLE_PROPERTY (1): [Is, Equals] NEVER.

【问题讨论】:

  • 什么是“make”类型?是Car吗?我对此表示怀疑。因此,您的查询不应返回 List。它也不应该使用 SQL,而应该使用 JPQL。品牌与汽车有什么关系?为了编写正确的查询,了解这一点很重要吗?贴出 Car 实体的代码。
  • 刚刚发布了 Car 实体。 makeString。谢谢。
  • 不...确定您为什么要在此处创建本机查询。您可以在没有本机查询的情况下使用findAll;您遇到了一些 Spring 语法糖,其中生成您的方法的进程看到“By”并且因为您没有为该方法提供参数而吓坏了。
  • 为什么car.make 是一个字符串?不是汽车品牌吗?如果是这样,为什么不把它也作为一个实体呢?

标签: java spring-boot jpa


【解决方案1】:

Makes 是字符串。所以如果你想让你的方法返回一个品牌列表,它的返回类型应该是List&lt;String&gt;,而不是List&lt;Car&gt;

而且查询需要选择品牌,而不是汽车:

select distinct car.make from Car car

【讨论】:

  • 谢谢.. 试过这个并得到一个百里香叶错误。我正在尝试了解 Spring Data JPA 中的查询,但不确定从哪里开始,因为似乎有很多查询方法。寻找指南、示例、教程或 Spring Docs 之类的东西要么对我没有帮助,要么目前超出我的想象。
  • thymeleaf 错误与 JPA 查询无关。您需要做的是阅读错误消息和堆栈跟踪以及有关它的原因。找不到教程。
【解决方案2】:

尚未在我的笔记本电脑上运行此程序,但如果您尝试: 向 CarController 添加构造函数:

public CarController(CarRepository carRepository){
this.carRepository = carRepository;
}

然后可能只是尝试:

public List<Car> findAll();

在存储库中?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 2015-08-04
    • 2018-09-23
    相关资源
    最近更新 更多