users@glassfish.java.net

Provide arrays or collections as NamedQuery parameter

From: <glassfish_at_javadesktop.org>
Date: Thu, 23 Aug 2007 08:10:39 PDT

Dear Community,

is there any way in JPA to provide arrays or collections as parameter for a named query? The thing is I only want to design mass data interfaces in my EJB's, similar to the following example:

Collection<MyEntity> getMyEntityByIds(int[] ids);

In MyEntity I would like to provide a named query like:

@NamedQuery(name = "MyEntity.findByIds", query = "SELECT e FROM MyEntity e WHERE e.id IN (:ids)")

and implement the bean method like this:

public Collection<MyEntity> getMyEntityByIds(int[] ids) {
        Query qry = em.createNamedQuery("MyEntity.findByIds");
        qry.setParameter("ids", ids);
        return qry.getResultList();
}

This won't work as the ids parameter is expecting a single Integer. My second try did not work neither, which provided a comma separated string of ids to the parameter. But: querying a single fetch query while looping over the given id's would cause a serious performance bottelneck which I want to avoid.

Does anyone have an idea? I know that JPA accepts queries like

SELECT e FROM MyEntity e WHERE e.id IN (1,2,3)

or

SELECT e FROM MyEntity e WHERE e.id IN (:param1, :param1, :param3)

but this is certainly unusable as in most cases you won't know the count of ids needed by the application.

Thank you in advance for any suggestions.
[Message sent by forum member 'senortorpedo' (senortorpedo)]

http://forums.java.net/jive/thread.jspa?messageID=232320