简体   繁体   中英

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:

@Enumerated(EnumType.STRING)

in the Trips table.

When I'm doing my HTTP request with exactly one category I'm getting the correct results. Same behaviour when doing a request without categories key.

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

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

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

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:

@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

t.category IN (:categories)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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