我需要一些帮助...
我实际上正在构建一个应用程序并使用JPA进行查询,但枚举类型有问题。
我有这个。
public enum SchedulingStatus {
SKIPPED, PENDING_CONFIRMATION, CONFIRMED, SENT, PROCESSING, CANCELLED, FINISHED, FINISHED_WITH_ERRORS, NOT_CONFIRMED_IN_SERVER
}
然后类FarmIrrigationScheduling.java与许多其他字段和重要的一个:
@Column
@Enumerated(EnumType.ORDINAL)
private SchedulingStatus status;
然后尝试执行此查询:
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);
我收到以下异常:
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
我并不确切地说为什么它不能将Enum强制转换为java.lang.Enum。在我使用Hibernate和HQL之前我正在工作,现在我改变了这个问题。
有谁知道为什么以及如何解决它?
提前多多感谢!!
答案 0 :(得分:3)
这与枚举无关。
您必须传入List而不是数组:
// Create a List:
List<SchedulingStatus> statuses = Arrays.asList(SchedulingStatus.FINISHED,
SchedulingStatus.FINISHED_WITH_ERRORS, SchedulingStatus.CANCELLED);
query.setParameter("statusCollection", statuses);