无法在JPA查询中应用where条件子句

时间:2018-11-24 00:04:57

标签: java hibernate spring-boot jpa spring-data-jpa

我在ProductRepository中写了一个查询,如下所示

@Query(value = "select p from Product p where p.code = :code")
Product findByCode(@Param("code")String code);

很明显,我只希望查询返回1个产品,但是当我调用API时,它总是返回我所有产品的列表。

这是我从日志中得到的查询

Hibernate: select product0_.code as code1_4_, product0_.name as name2_4_, product0_.price as price3_4_, product0_.unit as unit4_4_ from product product0_

我看不到我写的WHERE条件子句

这是我的产品类别:

package com.example.model;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.util.List;
import java.util.Objects;

@Entity
public class Product  {
@Id
private String code;
private String name;
private Float price;
private String unit;
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "product", cascade         
=CascadeType.ALL)
private List<OrderLine> orderLines;
public Product(String code, String name, float price, String unit) {
    this.code = code;
    this.name = name;
    this.price = price;
    this.unit = unit;
}


public Product(String code, float price, String unit) {
    this.code = code;
    this.price = price;
    this.unit = unit;
}

public Product(String code) {
    this.code = code;
    this.price = Float.valueOf(0);
}

public Product(String code, String unit) {
    this.code = code;
    this.unit = unit;
    this.price = Float.valueOf(0);
}

public Product() {}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


public Float getPrice() {
    return price;
}

public void setPrice(float price) {
    this.price = price;
}

public String getUnit() {
    return unit;
}

public void setUnit(String unit) {
    this.unit = unit;
}

@Override
public String toString() {
    return "Product{" +
            "code='" + code + '\'' +
            ", name='" + name + '\'' +
            ", price=" + price +
            ", unit='" + unit + '\'' +
            '}';
}

public List<OrderLine> getOrderLines() {
    return orderLines;
}

public void setOrderLines(List<OrderLine> orderLines) {
    this.orderLines = orderLines;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Product)) return false;
    Product product = (Product) o;
    return this.getCode().equals(product.getCode());
}

@Override
public int hashCode() {
    return Objects.hash(getCode());
}}

我的控制器

@RestController
@RequestMapping("/products")
public class ProductController {

@Autowired
ProductRepo productRepo;
@Autowired
StaffRepo staffRepo;
@Autowired
InventoryRepo inventoryRepo;
@GetMapping("/get")
public ResponseEntity getAll(){
    List<Product> products = productRepo.findAll();
    if(products== null){
        return ResponseEntity.status(404).body("Product not found !");
    }
    return ResponseEntity.ok().body(products);
}
@GetMapping("/get/{code}")
public ResponseEntity getProductByCode(@PathVariable String code){
    Product p = productRepo.findByCode(code);
    if(p== null){
        return ResponseEntity.status(404).body("Product not found !");
    }
    return ResponseEntity.ok().body(p);
}

0 个答案:

没有答案
相关问题