簡體   English   中英

Spring Data(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表中。

當我只用一個類別執行HTTP請求時,我得到正確的結果。 沒有類別鍵的請求時的行為相同。

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

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

當使用多個類別時:

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

我收到以下異常:

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

有人知道我在做什么錯嗎?

請嘗試以下方法之一:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM