How to pass an array of numbers as a parameter in Jpa Hibernate Query?

Viewed 3662

Example :

Select * from student where roll_no in ( 1,2,3 );

In student repository (Spring Boot):

@Query(value="Select * from student where roll_no in (?)",native =true)
List selectStudents(What do I give here ?)

Or is there any other way to implement this?

2 Answers

If you want to use native query you can do it like below

@Query(value="select * from student where roll_no in (:rollNos)",native =true)
List<Object[]> selectStudents(@Param("rollNos") List<Integer> rollNos);

But I would recommend you to do it using JPA named query like below which is very easy to handle further as it gives you the result in entity format.

Student findByRollNo(List<Integer> rollNos);
Related