EJB QL: Passing array of enum to SELECT * WHERE a IN () clause

Hello,

I am trying to pass an array of enum to EJB QL as followed:

em.createQuery("FROM OrderInfo o WHERE o.ordStatus IN (:ordStatus)")

.setParameter("ordStatus", ordStatsStr)

Note that o.ordStatus is of type enum OrderStatus.

I've tried to set "ordStatus" as String (i.e. 'OPEN', 'CLOSED'), as well as an array of enum but EJB QL would not parse them (perhaps for a good reason). EJB QL (I use JBoss + Hibernate) threw the following exception (or similar depending on whether I passed in an array of enum or a String):

org.hibernate.TypeMismatchException: named parameter [ordStatus] not of expected type;

expected =class ....OrderStatus; but was =java.lang.String

Can someone show me the way to set the IN clause with an array of enum?

Thanks

[895 byte] By [Tony_Maia] at [2007-11-27 5:10:44]
# 1

I suppose that your enum declaration is like

public enum OrderStatus{

OPEN,

CLOSED

};

The exception remarks that the valid type must be OrderStatus. I think that you must change your sentence by

em.createQuery("FROM OrderInfo o WHERE o.ordStatus IN (:ordStatus)")

.setParameter("ordStatus", OrderStatus.OPEN);

On the other hand, for the array option I think that you must use setParameterList like this

OrderStatus options[] = new OrderStatus[2];

options[0] = OrderStatus.OPEN;

options[1] = OrderStatus.CLOSED;

em.createQuery("FROM OrderInfo o WHERE o.ordStatus IN (:ordStatus)")

.setParameterList("ordStatus", options);

I hope it helps you

hayken.

haykena at 2007-7-12 10:30:57 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
It seems that Query.setParameterList is a Hibernate's function and not of a Java Persistence. Is that right?I have tried very hard thus far not to bind to a specific vendor's technology. Is there a way to do it without using Hibernate's method?Thanks
Tony_Maia at 2007-7-12 10:30:57 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Yes, that's right. It`s a hibernate function. But you told that your are using JBoss + hibernate. In EJB 3.0 standard that method doesn磘 exist. I use toplink and I haven磘 that function. You can see it
haykena at 2007-7-12 10:30:57 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
To use pure EJB3, I've learned, is to pass in a List, not an array.Thanks
Tony_Maia at 2007-7-12 10:30:57 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...