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

1
  • 1
    I think you are supposed to separate question from answer using the checkbox at the bottom for that : stackoverflow.com/help/self-answer Commented Apr 27, 2018 at 12:54

2 Answers 2

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 :)

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

Comments

2

If you have a list of strings which are to be passed as parameter for an IN clause:

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

1 Comment

Can you please edit your answer to contain an explanation?

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.