Search code examples
javaspringhibernatejpaspring-data

How to customize parameters the way parameters list is built in Spring JPA?


I'm trying to invoke DB function with a method in Spring JPA repository like this:

@Query(value = "select * test.getVals(:ids)", nativeQuery = true)

When I invoke it manually, I do:

select * from test.getVals(array[2, 14577])

However, SQL that is being generated looks differently:

select * from test.getVals(?, ?, ?)

This syntax doesn't work. Is there a good way to customize how these parameters are converted into SQL? so that I could have array[?, ?]?

If I do:

array[:ids]

It's converted with () like this:

array[(?, ?)]

Solution

  • array[:ids]
    

    If we take the list it converts into comma-separated values enclosed by parenthesis.

    array[(?, ?)]
    

    Alternative to this we can use the string value

    List<Integer> ids = new List<Integer>{1,2,3};
    String idsString = string.Join(",", ids);
    

    This is we get

    arry[:idsString] => array[?,?]