1

While using spring-data-jdbc and postgresql to store and search data, I was not able to come up with a query inside @Query annotation which can cover collections which are nulls. I mean the following simplified example:

@Query("SELECT * FROM dialects WHERE type = 'Artificial' AND (:languages IS NULL OR language IN (:languages))")
findAllByTypeAndLanguages(@Param("languages") List<String> languages);

When I call the repository with languages = null, I get an error: org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $1

I didn't find an example in the documentation how this can be done. Of course, I can handle null values in the java code and call repository methods accordingly, but in my case the search can have a few collections as input parameters and it will be a mess if I cover each case with separate query..

I tried to use also String arrays and it didn't work. However, it seems to work with List, so does it mean that spring-data-jdbc cannot work with collections of objects?

1 Answer 1

0

The query should have next one body:

@Query("SELECT * FROM dialects WHERE type = 'Artificial' AND ((:languages) IS NULL OR language IN (:languages))")
findAllByTypeAndLanguages(@Param("languages") List<String> languages);

Because when you have a collection of parameters that can be NULL, the query parameter also should be in ().

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but this didn't help as I expect that the collection itself can be null
what do you mean, if collection is null nothing will be returned instead of error that was previously
When I call findAllByTypeAndLanguages(null) I get this: 2023-09-09 10:29:01 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.jdbc.BadSqlGrammarException: org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $1 When the languages collection is not null, it works fine. My idea is that if the collection is null, only type filter should be applied.
can you also try to execute this sql in native version ? In a pgAdmin for example and try to pass there a param with null value.

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.