I have 3 tables: user
, privilege
and user_privilege
. Last table stores user_id
and privilege_id
, i.e. information about users and their privileges. I want to filter users by privileges. For this, I am passing list of privilege ID's List<Integer> privilegeIDs
as parameter to the query below:
@NamedNativeQuery(name = "User.filterUsersByPrivilges",
query = "SELECT u.username " +
"FROM user u " +
"LEFT JOIN user_privilege up ON u.id = up.user_id " +
"WHERE up.privilege_id IN ?1; ",
resultSetMapping = "userCatalog")
However, on client side (Angular 5), sometimes I may leave this privilege list empty in order to retrieve all users, no matter if they have a privilege or not. Here I have found out that if I pass empty list parameter to IN
condition, it gives an error in server logs. It turns out when checking with the IN
condition, parameter cannot be empty.
I have tried to pass null
and check with WHERE up.privilege_id IN ?1 OR ?1 IS NULL
but it still swears that you can not pass an empty list to IN
.
Now, the question is: how I must check if list is empty so that I can retrieve all users even if they have or do not have privileges; at the same times, if parameter is not empty - retrieve those users that has privileges specified in the list parameter.
If you have experience in solving a similar problem, help me, please. Thanks in advance!