Собственный запрос с критериями фильтрации

Пробуем реализовать поиск с фильтрами. Наше тело запроса { "channelId": 3}, и оно дает записи, которые имеют идентификатор канала с 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)

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


person Süresh AK    schedule 31.01.2020    source источник


Ответы (1)


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

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)")
person vivekkurien    schedule 31.01.2020
comment
org.postgresql.util.PSQLException: ОШИБКА: оператор не существует: bigint = bytea Подсказка: ни один оператор не соответствует данному имени и типу(ам) аргумента. Возможно, вам потребуется добавить явное приведение типов. - person Süresh AK; 31.01.2020