【问题标题】:Spring Data JPA - Query with no argumentsSpring Data JPA - 不带参数的查询
【发布时间】:2016-10-04 00:33:22
【问题描述】:

我想进行一个查询来比较我的表内部的 2 列,因此无需向查询添加参数。例如:我有一个表 Product,我想查找所有具有 stock 的产品

+----+-----------+-------+-----------+
| ID |   NAME    | STOCK | STOCK_MIN |
+----+-----------+-------+-----------+
|  1 | product 1 |     2 |         3 |
|  2 | product 2 |     3 |         1 |
|  3 | product 3 |     4 |         5 |
+----+-----------+-------+-----------+

如何使用 Spring Data JPA 做到这一点?

在我的 ProductRepository 中声明这样的内容会引发错误

List<Product> findByStockIsLessThanEqualStockMin();

这也行不通

@Query("select p from product p where p.stock <= p.stockmin")
List<Product> findByStockIsLessThanEqualStockMin();

Spring Data JPA documentation 中的所有示例都在查询中使用了一些参数,但我不想这样做,因为我的 Product 表中的每一行的 stockMin 属性都不同。

[已修复]
问题非常愚蠢。我忘记将我的实体大写,select 不是必需的。下面的查询工作正常:

@Query("from Product p where p.stock <= p.stockMin")
List<Product> findByStockIsLessThanEqualStockMin();

pd:这是我在这里的第一个问题。谢谢大家! :)

【问题讨论】:

标签: java mysql spring spring-data spring-data-jpa


【解决方案1】:

下面提到的代码工作正常。您的实体类中的 可能存在问题。

package com.demo.account.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import com.fasterxml.jackson.annotation.JsonInclude;

@Entity
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Product {

    @Id
    @GeneratedValue(
        strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private Integer stock;

    private Integer stockMin;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        this.stock = stock;
    }

    public Integer getStockMin() {
        return stockMin;
    }

    public void setStockMin(Integer stockMin) {
        this.stockMin = stockMin;
    }

    public Product() {

    }

    public Product(String name, Integer stock, Integer stockMin) {
        super();
        this.name = name;
        this.stock = stock;
        this.stockMin = stockMin;
    }

}


package com.demo.account.dao;

import java.util.List;

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

import com.demo.account.model.Product;

@Repository
public interface ProductDao extends CrudRepository<Product, Long> {

    @Query("from Product p where p.stock <= p.stockMin")
    List<Product> findByStockIsLessThanEqualStockMin();

}

Product product = new Product("Mobile",10,3);
            productDao.save(product);

            product = new Product("Curtains",15,15);
            productDao.save(product);

            product = new Product("Clothing",5,15);
            productDao.save(product);

            System.out.println(productDao.findByStockIsLessThanEqualStockMin());

【讨论】:

  • 实体类没问题,但你的答案给了里面!。我的问题是我在@Query 中使用了product 而不是Product。谢谢!
猜你喜欢
  • 2018-08-16
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 2020-02-11
  • 2017-04-21
  • 2015-08-04
相关资源
最近更新 更多