druiz druiz - 2 months ago 21
Java Question

JPA Query: java.lang.ClassCastException: [Enum] cannot be cast to java.lang.Enum

I need some help...

I am actually building an application and using JPA for the queries but there is something wrong with the enum types.

I have this.

public enum SchedulingStatus {

SKIPPED, PENDING_CONFIRMATION, CONFIRMED, SENT, PROCESSING, CANCELLED, FINISHED, FINISHED_WITH_ERRORS, NOT_CONFIRMED_IN_SERVER

}


Then the class FarmIrrigationScheduling.java with many other fields and the important one:

@Column
@Enumerated(EnumType.ORDINAL)
private SchedulingStatus status;


And then trying to do this query:

TypedQuery<FarmIrrigationScheduling> query = getEntityManager().createQuery(
"from FarmIrrigationScheduling where farm = :farm AND status not in :statusCollection ORDER BY calculated DESC",
FarmIrrigationScheduling.class);
query.setParameter("farm", farm);

SchedulingStatus[] status = new SchedulingStatus[] { SchedulingStatus.FINISHED,
SchedulingStatus.FINISHED_WITH_ERRORS, SchedulingStatus.CANCELLED };
query.setParameter("statusCollection", status);

return getSingleResult(query);


I am getting the following exception:

Caused by: java.lang.ClassCastException: [Lau.com.agrichem.addams.server.model.enums.SchedulingStatus; cannot be cast to java.lang.Enum
at org.hibernate.type.EnumType.nullSafeSet(EnumType.java:239)
at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:170)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:53)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:628)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1956)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1909)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1887)
at org.hibernate.loader.Loader.doQuery(Loader.java:932)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
at org.hibernate.loader.Loader.doList(Loader.java:2615)
at org.hibernate.loader.Loader.doList(Loader.java:2598)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2430)
at org.hibernate.loader.Loader.list(Loader.java:2425)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:502)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1458)
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1426)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1398)
at org.hibernate.Query.getResultList(Query.java:417)
at au.com.agrichem.addams.server.model.repositories.base.HibernateDaoImpl.getSingleResult(HibernateDaoImpl.java:100)
at au.com.agrichem.addams.server.model.repositories.impl.SchedulingsDaoImpl.getCurrentSchedulingByFarm(SchedulingsDaoImpl.java:126)
at au.com.agrichem.addams.server.services.impl.SchedulingsServiceImpl.getScheduling(SchedulingsServiceImpl.java:68)
... 116 more


I don't exactly why it cannot cast the Enum to actually a java.lang.Enum. Before I was using Hibernate and HQL for this and I was working, now that I changed I am having this issue.

Does anyone know why and how I can fix it??

Thanks a lot in advance!!

Answer

This has nothing to do with enums.

You must pass in a List, not an array:

// Create a List:
List<SchedulingStatus> statuses = Arrays.asList(SchedulingStatus.FINISHED,
        SchedulingStatus.FINISHED_WITH_ERRORS, SchedulingStatus.CANCELLED);

query.setParameter("statusCollection", statuses);