首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可选的hql查询参数

问可选的hql查询参数
EN

Stack Overflow用户
提问于 2017-07-11 16:36:32
回答 1查看 792关注 0票数 0

我有一个rest存储库:

代码语言:javascript
复制
@Query(
            "SELECT ord FROM Order as ord where " +
                    " (:#{#customer} is null or :#{#customer}='' or  ord.customer = :#{#customer}) and " +
                    " (:#{#supplier} is null or :#{#supplier}='' or ord.supplier = :#{#supplier} ) and " +
                    " (:#{#startDate} is null or :#{#startDate}='' or ord.date >= :#{#startDate} ) and " +
                    " (:#{#endDate} is null or :#{#endDate}='' or ord.date >= :#{#endDate} )"
    )
    Page<Order> query(
            @Param("customer") Organization customer,
            @Param("supplier") Organization supplier,
            @DateTimeFormat(pattern = "dd-MM-yyyyy") @Param("startDate") Date startDate,
            @DateTimeFormat(pattern = "dd-MM-yyyyy") @Param("endDate") Date endDate,
            Pageable pageable
    );

当我在没有任何参数的情况下调用查询时,它工作得很好,并且返回每个订单。如果我提供了任何一个参数,而没有提供其余的参数,我就会得到org.hibernate.QueryException: Not all named parameters have been set。对此有解决方案吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-11 19:32:53

您混合了实体名称和绑定参数的SpEL。对于绑定使用':parameterName‘而不是':#{#parameterName}’

代码语言:javascript
复制
@Query(
            "SELECT ord FROM Order as ord where " +
                    " (:customer is null or :customer='' or  ord.customer = :customer) and " +
                    " (:upplier is null or supplier='' or ord.supplier = :supplier) and " +
                    " (:startDate is null or :startDate='' or ord.date >= :startDate) and " +
                    " (:endDate is null or :endDate='' or ord.date >= :endDate )"
    )
    Page<Order> query(
            @Param("customer") Organization customer,
            @Param("supplier") Organization supplier,
            @DateTimeFormat(pattern = "dd-MM-yyyyy") @Param("startDate") Date startDate,
            @DateTimeFormat(pattern = "dd-MM-yyyyy") @Param("endDate") Date endDate,
            Pageable pageable
    );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45029417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档