Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing List<Integer> in spring data jpa native query

Using spring data JPA, I am trying to make this sort of query (it is more complex, this is a simple case)

@Query(nativeQuery = true, 
       value = "SELECT * FROM events WHERE typeId IN (?1)")
List<Event> findEventsByType(List<Integer> types);

When I launch the query, an exception raises:

org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.util.ArrayList. Use setObject() with an explicit Types value to specify the type to use.

I have tried List < Integer >, Integer[], Object[] and String but it is not working...

Can't I pass list of values?

Which is the best approach to make this sort of queries?

Thanks!

like image 250
Antonio Acevedo Avatar asked Dec 02 '25 10:12

Antonio Acevedo


2 Answers

Try taking away the @Query and make the method name:

public List<Event> findByTypeIn(List<Integer> types);

See table 2.2 in the link: http://docs.spring.io/spring-data/jpa/docs/1.2.0.RELEASE/reference/html/

like image 101
Thomas Turner Avatar answered Dec 05 '25 01:12

Thomas Turner


I tried like below and it works for me.

@Query(value = "select * from events where type_id in :types", nativeQuery = true)
List<Event> findEventsByType(@Param("types") List<Integer> types);
like image 31
Sahil Chhabra Avatar answered Dec 04 '25 23:12

Sahil Chhabra