Raikyn
Raikyn

Reputation: 163

IN clause to select all elements if list is empty/null

I have the follow @Query in my repository :

@Query(value = "SELECT * FROM tableA a WHERE a.id IN :listId ",
    nativeQuery = true)
public Page<ObjectA> findInById(@Param(listId) List<String> listId);

And I would like to select all elements if listId is empty or null.

How can I do that? If possible I'd like to keep the @Query.

Upvotes: 1

Views: 824

Answers (1)

MikNiller
MikNiller

Reputation: 1280

Just add an or condition, like

@Query(value = "SELECT * FROM tableA a 
    WHERE (a.id IN :listId or coalesce(:listId , null)  is null)",
    nativeQuery = true)
public Page<ObjectA> findInById(@Param(listId) List<String> listId);

Upvotes: 2

Related Questions