Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List as a named parameter in JPA query using TopLink

Tags:

java

jpa

toplink

In the following JPA query, the :fcIds named parameter needs to be a list of integer values:

@NamedQuery(name = "SortTypeNWD.findByFcIds", query = "SELECT s FROM SortTypeNWD s WHERE s.sortTypeNWDPK.fcId IN (:fcIds)")

Quite logically, this is what is done when the named query is called:

Query findByDatesPlFcIds = em.createNamedQuery("SortTypeNWD.findByFcIds");
findByDatesPlFcIds.setParameter("fcIds", fcIds);

Where the variable fcIds is an ArrayList containing integers.

All the above code works fine with Hibernate but doesn't with TopLink:

Caused by: java.lang.IllegalArgumentException: You have attempted to set a value of type class java.util.ArrayList for parameter fcIds with expected type of int from query string SELECT s FROM SortTypeNWD s WHERE s.sortTypeNWDPK.fcId IN (:fcIds).

Is there a workaround to use a List as a named parameter in TopLink? Can the type of the named parameter be forced?

like image 239
Gilles Avatar asked Jul 23 '09 07:07

Gilles


1 Answers

Toplink implements JPA 1.0 which doesn't support passing a list as a parameter (collection_valued_input_parameter is the term used in documentation). This is supported in JPA 2.0 which is implemented in TopLink's successor, EclipseLink.

If you have to stick with TopLink then you need to write a loop to include each item on the list as a parameter.

like image 186
Loren_ Avatar answered Oct 26 '22 02:10

Loren_