gpt4 book ai didi

java - Spring数据(REST)和@Query : optional list as param

转载 作者:行者123 更新时间:2023-11-30 02:39:21 27 4
gpt4 key购买 nike

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

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

行程表中。

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

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

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

当使用多个类别时:

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

我遇到以下异常:

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_)) ]

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

最佳答案

尝试以下方法之一:

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)

关于java - Spring数据(REST)和@Query : optional list as param,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42225877/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com