简体   繁体   English

Spring Data(REST)和@Query:可选列表作为参数

[英]Spring Data (REST) & @Query: optional list as param

I'm having the following function in one of my repositories: 我的一个存储库中具有以下功能:

@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);

The Category is an enum which is stored as: Category是一个枚举,存储为:

@Enumerated(EnumType.STRING)

in the Trips table. 在“ Trips表中。

When I'm doing my HTTP request with exactly one category I'm getting the correct results. 当我只用一个类别执行HTTP请求时,我得到正确的结果。 Same behaviour when doing a request without categories key. 没有类别键的请求时的行为相同。

htt*://localhost/filter?categories=PRIVATE ==> ok htt*://localhost/filter?categories=PRIVATE ==>确定

htt*://localhost/filter ==> ok htt*://localhost/filter ==>确定

When using more than one category: 当使用多个类别时:

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

I'm getting the following exception: 我收到以下异常:

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: {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_ IS NULL OR t.category IN (:categories_0_, :categories_1_)) ] org.hibernate.hql.internal.ast.QuerySyntaxException:意外的AST节点:{vector} [从foo.bar.services.trips.model.Trip t WHERE中选择count(t)(:from IS NULL或t.startTs> = :from)AND(:categories_0_,:categories_1_是NULL或t.category IN(:categories_0_,:categories_1_))]

Anybody have an idea what I'm doing wrong here? 有人知道我在做什么错吗?

Try one of the following: 请尝试以下方法之一:

1) Try to enclose the statements involving the list in parentheses: 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) Enclose the :categories in parentheses here 2)在此处用括号括住:类别

t.category IN (:categories)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM