2017-02-14 63 views
1

我有我的库之一以下功能:春数据(REST)@Query:可选列表为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表中。

当我正在做我的http请求只有一个类别我得到正确的结果。执行没有类别键的请求时的行为相同。

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

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

当使用多个类别:

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

,我发现了以下异常:

org.hibernate.hql.internal.ast.QuerySyntaxException:意外的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或t.category IN(:categories_0_, :categories_1_))]

任何人有一个想法,我做错了什么吗?

回答

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)括起来:类别这里括号

t.category IN (:categories) 
+1

试过但都没有工作。但是你指出了我正确的方向。这是最后的工作:AND((:categories)IS NULL OR(t.category IN(:categories))。Thx! – StephanM

+0

太棒了!很高兴我能帮到你 –