HQL IN operator, Array of Enums ClassCastException
Asked Answered
S

2

1

Here is my stripped down class and enum.

class A
{
    @Enumerated (value = EnumType.STRING)
    AType type;
}

enum AType
{
    X,Y
}

if I run

query = FROM A a WHERE a.type = :type
query.setParameter("type", AType.X);

All is fine and dandy.

However if I do the following:

AType[] types = new AType[1];
types[0] = AType.X;
query = FROM A a WHERE a.type IN (:types)
query.setParameter("types", types);

I get:

Lcom.src.AType; cannot be cast to java.lang.Enum

If I do:

Enum[] types = new Enum[1];
types[0] = AType.X;
query = FROM A a WHERE a.type IN (:types)
query.setParameter("types", types);

I get:

Ljava.lang.Enum; cannot be cast to java.lang.Enum

Any ideas?

Surfacetosurface answered 22/2, 2011 at 21:1 Comment(0)
T
3
query.setParameterList("types", types);
                  ^--

This should do it.

Thanatos answered 22/2, 2011 at 21:29 Comment(2)
Such an obvious oversight on my part. Thanks!Surfacetosurface
The signature of the abovementioned method is setParameters(Object[] values, Type[] types) (Hibernate 3.5.4). This does not seem to fit. The method setParameterList(String name, Object[] vals) is the right one.Commorancy
L
3

query.setParameterList("types", types);

Lynnelle answered 16/2, 2012 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.