persistence@glassfish.java.net

Re: Problem binding collections to queries

From: Wouter van Reeven <wouter_at_van.reeven.nl>
Date: Thu, 7 Feb 2008 10:15:15 +0100

Hi,

On Thu, Feb 07, 2008 at 09:57:31AM +0100, Christian Kerwer wrote:
> I’m trying to bind a collection of Ids into a Query like this:
>
> Select p from Project p where p.id IN (:IdList)
>
> with IdList as Array of Long and field p.id of type Long.
>
> It will be quitted with the following exception:
>
> java.lang.ClassCastException:
>
> You have attempted to set a value of type class [Ljava.lang.Long; for parameter
> planningObjectIdList with expected type of class java.lang.Long from query
> string select planObjStrLnk from PlanningObjectStructureLink planObjStrLnk
> where planObjStrLnk.planningClassStructure.id = :planningClassStructureId and
> planObjStrLnk.planningObject.id in (:planningObjectIdList)

The EJB Persistence spec states that the parameter types on both sides of the IN
operator must be of the same type. In other words, you cannot have a Long on one
side and an Array on the other side. Either you'll have to write out every id in
the Array, like this

p.id IN (12, 134, 66334567884)

or you'll have to rewrite your query.



HTH, Wouter van Reeven

-- 
People: "If she weighs the same as a Duck, she's made of wood!"
Sir Bedevere: "And therefore...?"