【问题标题】:hql query with param and pageable error "Parameter with that position [1] did not exist"带有参数和可分页错误的 hql 查询“具有该位置 [1] 的参数不存在”
【发布时间】:2016-09-15 06:46:59
【问题描述】:

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

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. 将 date = :date 部分更改为 date = ?2(仍然是同样的错误)

你能帮我解决这个问题吗?

【问题讨论】:

    标签: sql spring-data hql


    【解决方案1】:

    我终于找到了解决方案。这是我愚蠢的错误。我忘了向 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);
    

    【讨论】:

      猜你喜欢
      • 2019-03-06
      • 2015-11-22
      • 2018-07-05
      • 2019-07-03
      • 1970-01-01
      • 2016-09-11
      • 2017-07-29
      • 1970-01-01
      • 2017-10-03
      相关资源
      最近更新 更多