Spring数据jpql(休眠)查询无法在MySQL数据库中运行错误:1241

时间:2019-04-15 12:36:54

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

我已经在Spring数据JPA中使用jpql查询实现了过滤器。

我正在使用此类作为过滤器;

@NoArgsConstructor
@Getter
@Setter
@ToString
public class FilterTO {
    private Double priceMin;
    private Double priceMax;
    private List<Long> categories;
    private List<String> brands;

    public FilterTO(Double priceMin, Double priceMax, List<Long> categories, List<String> brands) {
        this.priceMin = (priceMin == null)? 0 : priceMin;
        this.priceMax = (priceMax == null)? Double.MAX_VALUE: priceMax;
        this.brands = (brands == null || brands.isEmpty())? null : brands;
        this.categories = (categories == null || categories.isEmpty())? null : categories;
    }
}

这是我正在使用的方法和查询;

@Query("SELECT p FROM Product p WHERE " +
            "p.price * (1 - p.discount) BETWEEN :#{#filter.getPriceMin()} AND :#{#filter.getPriceMax()} " +
            "AND ( p.brand IN :#{#filter.getBrands()} OR :#{#filter.getBrands()} IS NULL) " +
            "AND ( p.category.id IN (SELECT DISTINCT c.id FROM Category c " +
            "WHERE c.parent.id IN :#{#filter.getCategories()}) OR p.category.id IN :#{#filter.getCategories()} OR :#{#filter.getCategories()} IS NULL) ")
    Page<Product> findByFilter(@Param("filter") FilterTO filter, Pageable pageable);

这是一个干净版本的查询:

SELECT p FROM Product p WHERE p.price * (1 - p.discount) BETWEEN :#{#filter.getPriceMin()} AND :#{#filter.getPriceMax()} 
AND ( p.brand IN :#{#filter.getBrands()} OR :#{#filter.getBrands()} IS NULL) 
AND ( p.category.id IN (SELECT DISTINCT c.id FROM Category c WHERE c.parent.id IN :#{#filter.getCategories()}) OR p.category.id IN :#{#filter.getCategories()} OR :#{#filter.getCategories()} IS NULL)

所以基本上,我会过滤价格,类别和品牌。如果类别列表为空,则忽略它;如果品牌列表为空,则也忽略它。

该查询在H2中也可以正常工作,但是当我们尝试在MySQL上投入生产时,会出现以下错误: java.sql.SQLException:操作数应包含1列

0 个答案:

没有答案