Spring 数据(REST)和@Query:作为参数的可选列表
Spring Data (REST) & @Query: optional list as param
我的一个存储库中有以下功能:
@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
table.
当我对一个类别执行 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)
我的一个存储库中有以下功能:
@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
table.
当我对一个类别执行 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)