4

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?

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Tried both but weren't working. But you pointed me to the right direction. This is what finally is working: AND ((:categories) IS NULL OR (t.category IN (:categories)). Thx!
Great! Glad i could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.