Skip to content
Advertisement

How to check collection for null in spring data jpa @Query with in predicate

I have this query in my spring data jpa repository:

When I run this query, it shows me this error by console:

However if I run the query without the (COALESCE (?2) IS NULL OR part, just the table1.code IN ?2, it does work

Does anyone know what this error could be due to?

Advertisement

Answer

  1. COALESCE with one parameter does not make sense. This is an abbreviated CASE expression that returns the first non-null operand. (See this)

  2. I would suggest you to use named parameters instead of position-based parameters. As it’s stated in the documentation this makes query methods a little error-prone when refactoring regarding the parameter position.

  3. As it’s stated in documentation related to the IN predicate:

The list of values can come from a number of different sources. In the constructor_expression and collection_valued_input_parameter, the list of values must not be empty; it must contain at least one value.

  1. I would suggest you also avoid to use outdated Date and use instead java 8 Date/Time API.

So, taken into account all above, you should use a dynamic query as it was suggested also in comments by @SimonMartinelli. Particularly you can have a look at the specifications.

Assuming that you have the following mapping:

you can write the following specification:

and then use it:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement