3

folks,

I have a problem finding a way to send parameters for IN clause in a native query through Spring data repository. I tried sending list of strings, concatenated and formatted strings... nothing helped

a.valchev
  • 388
  • 4
  • 10
  • 1
    I think you are supposed to separate question from answer using the checkbox at the bottom for that : https://stackoverflow.com/help/self-answer – TheBakker Apr 27 '18 at 12:54

2 Answers2

4

Here is the solution:

    @Repository
public interface Repo extends PagingAndSortingRepository<Log, Long>
{
    @Query(value = QUERY, nativeQuery = true)
    List<OrderExecutionLog> findTop1OrderByInitiatedAsc(String[] types);

    String QUERY = "SELECT log.* " +
            "FROM log" +
            "WHERE `type` IN (?3) ";
}

I tried several variations with a list or concatenated strings and so on ... but a simple array gave me the desired result. I think that the Spring community need to take a look at this simple issue i encountered.

Good luck. Hope this helps someone like me :)

a.valchev
  • 388
  • 4
  • 10
2

If you have list of strings which are to be passed as parameter for an IN clause, refer below

@Query(value="SELECT * FROM SOME_TABLE WHERE COLUMN_SOME_NAME IN :someList", nativeQuery = true)
List<T> getList(List<String> someList);
Abhinav manthri
  • 151
  • 1
  • 14