【问题标题】:Spring Data (REST) & @Query: optional list as paramSpring Data (REST) & @Query:可选列表作为参数
【发布时间】:2017-02-14 12:12:36
【问题描述】:

我的一个存储库中有以下功能:

@RestResource(path = "filter", rel = "filter")
@Query("SELECT t "
        + "FROM Trip t "
        + "WHERE "
        + "(:from IS NULL OR t.startTs >= :from) "
        + "AND (:categories IS NULL OR t.category IN :categories) ")
Page<Trip> filter(
        @Param("from") Instant from,
        @Param("categories") List<Category> categories,
        Pageable pageable);

Category 是一个枚举,存储为:

@Enumerated(EnumType.STRING)

Trips 表中。

当我使用一个类别执行 HTTP 请求时,我得到了正确的结果。在没有类别键的情况下执行请求时的行为相同。

htt*://localhost/filter?categories=PRIVATE ==> 好的

htt*://localhost/filter ==> 好的

当使用多个类别时:

htt*://localhost/filter?categories=PRIVATE,BUSINESS

我收到以下异常:

org.hibernate.hql.internal.ast.QuerySyntaxException: 意外的 AST 节点:{vector} [select count(t) FROM foo.bar.services.trips.model.Trip t WHERE (:from IS NULL OR t.startTs >= :from) AND (:categories_0_, :categories_1_ 为 NULL 或 t.category IN (:categories_0_, :categories_1_)) ]

有人知道我在这里做错了什么吗?

【问题讨论】:

    标签: java spring hibernate jpa spring-data-rest


    【解决方案1】:

    尝试以下方法之一:

    1) 尝试将涉及列表的语句括在括号中:

    @Query("SELECT t "
            + "FROM Trip t "
            + "WHERE "
            + "(:from IS NULL OR t.startTs >= :from) "
            + "AND ((:categories IS NULL) OR (t.category IN :categories)) ")
    

    2) 在此处将 :categories 括在括号中

    t.category IN (:categories)
    

    【讨论】:

    • 两者都试过了,但都不起作用。但是你给我指出了正确的方向。这就是最终起作用的方法:AND ((:categories) IS NULL OR (t.category IN (:categories))。谢谢!
    • 太棒了!很高兴我能帮忙
    猜你喜欢
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    相关资源
    最近更新 更多