【发布时间】: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:这是我在这里的第一个问题。谢谢大家! :)
【问题讨论】:
-
不确定我能不能帮上忙,所以你试过 findAllBy... 吗?
-
你能试试这样吗:findByStockLessThanEqual(stockmin);参考:docs.spring.io/spring-data/jpa/docs/current/reference/html/…
-
请发布 Producto 类。查询取决于该实体的结构。
标签: java mysql spring spring-data spring-data-jpa