Собственный запрос с критериями фильтра - PullRequest
0 голосов
/ 31 января 2020

Мы пытаемся реализовать поиск по фильтрам. Тело нашего запроса: {"channelId": 3}, и оно дает записи, у которых channelId равен 3. Но когда я пытаюсь передать пустое тело запроса, {}, чтобы получить все записи без каких-либо критериев, возникает ошибка при загрузке пружины. , у нас есть строка в коде, подобном этой

Long channelId=  channelRequest.getChannelId() != null ? channelRequest.getChannelId():null;

, и в репозитории мы имеем эту ошибку

@Query(nativeQuery = true,value="select * " + 
        "from test_channel channelser0_ " + 
        + "where (:channelId is null or channelser0_.channel_id=:channelId)")
List<ChannelServiceMapping> getChannelIdAndServiceName(@Param("channelId") Long channelId); 

, которую мы получаем:

org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = bytea
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Position: 310
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308)

Пожалуйста, помогите мне решить эту проблему

1 Ответ

0 голосов
/ 31 января 2020

Вы можете использовать следующий код

coalesce(:channelId , null) is null

Запрос будет выглядеть как

@Query(nativeQuery = true,value="select * " + 
        "from test_channel channelser0_ " + 
        + "where (coalesce(:channelId , null) is null or channelser0_.channel_id=:channelId)")
...