带有param和可分页错误的hql查询“具有该位置的参数[1]不存在”

时间:2016-05-18 16:34:54

标签: sql spring-data hql

这是我的原生查询(遗憾的是我不能使用它,因为本机查询不适用于可分页)

select * from stock_indicators si inner join stock st on si.stock_id = st.id where si.slope_simple_regression_10 > 1 and si.date = (select date from stock_details order by date desc limit 1) order by si.slope_simple_regression_10 desc

这是一个相应的hql查询:

@Query(value = "from StockIndicators si join fetch si.stock where si.slopeSimpleRegression10Days > 1 and si.date = :date order by si.slopeSimpleRegression10Days desc", countQuery = "select count(si.stock) from StockIndicators si where si.slopeSimpleRegression10Days > 1")
    Page<StockIndicators> findWithStocksIndicators10DaysTrendUp(Pageable pageable, @Param("date") LocalDate date);

此查询不起作用,发生以下错误:

java.lang.IllegalArgumentException: Parameter with that position [1] did not exist

所以我到目前为止做了什么:

  1. 更改了参数的顺序(仍然是相同的错误)
  2. 将查询更改为包含子查询,但不幸的是HQL确实如此 不支持限制
  3. 将日期=:日期部分更改为日期=?2(仍然是相同的错误)
  4. 你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案。这是我的愚蠢错误。我忘了给countQuery添加参数。正确的方法应该是:

@Query(value = "from StockIndicators si join fetch si.stock where si.slopeSimpleRegression10Days > 1 and si.date = :date order by si.slopeSimpleRegression10Days desc", countQuery = "select count(si.stock) from StockIndicators si where si.slopeSimpleRegression10Days > 1 and si.date = :date ")
    Page<StockIndicators> findWithStocksIndicators10DaysTrendUp(Pageable pageable, @Param("date") LocalDate date);