19

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!

1
  • 2
    This is quite an old post, but did you manage to solve this? Commented Nov 7, 2016 at 8:34

9 Answers 9

14

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/

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

2 Comments

I can't do that because the real query is more complex. I have shown here the base case.
The title is NativeQuery but the query itself is a simple one. The answer works in case of Query, and not NativeQuery. Though, it would be helpful to people who land here with above scenarios.
8

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

Comments

7
@Query(value = "SELECT c from Company c where " +
    "c.companyName IN (:company_names)")
List<Company> findCompaniesByName(@Param("company_names") List<String> companyNames);

This is the solution to your problem.

Here I am passing List which contains company names and I am querying DB and storing result in List.

Hope this hepls!

Comments

6

Use JPQL. A native query is or should be passed to the database exactly as you have created the SQL string, and unless your driver can take a serialized collection and understand that the single parameter needs to be interpreted as many, it just won't work. The collection you pass in needs the SQL expanded from (?) to (?, ?,...) based on the number of elements in a collection, and JDBC drivers just are not able to do this, and JPA providers are required to execute the string as is.

A JPQL query allows the JPA provider to create the SQL it needs dynamically based on the list passed in, so it can expand the collection for you.

Comments

2

I know this is a little bit out of context (we use Update and not Select), but this can be usefull for others :


    /**
     * Update the state of list of entities using their ids
     * @param ids request ids
     * @param state new state
     * @return
     */
    @Modifying
    @Query(value = "UPDATE AbstractRequest SET state = :state WHERE id IN (:ids)")
    int updateStates(@Param("ids") List<Long> ids, @Param("state") InternalRequestStep state);

Comments

1

Try this. It will work for Native Query in SpringBoot JPA:

@Query(value = "SELECT * FROM table WHERE columnName IN (:inputList)" , 
     nativeQuery = true)
List<Object> findByObjectList(@Param("inputList") List<Object> inputList);

And also in case of JPA, try the below :

List<Object> findByObjectList(List<Object> inputList)

Comments

0

pass array this way inside IN Clause

@Query(value = "select name from teams where name in :names", nativeQuery = true)
List<String> getNames(@Param("names") String[] names);

Call this way

String[] names = {"testing team","development team"};
List<String> teamtest = teamRepository.getNames(names);

Comments

0

Remove brackets around (?1) parameter. Your query value should look like that:

@Query(value = "SELECT * FROM events WHERE typeId IN ?1")

Comments

-2

try querying like this:

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

did it work?

Comments

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.