Java:拥有@Enumerated(STRING)但仍然获得ClassCastException

时间:2016-08-04 09:51:58

标签: java enums entity classcastexception

我的实体有一个枚举字段:

@Entity
@NamedQueries({
    @NamedQuery(
        name = "MyEntity.findEnum",
        query = "SELECT myEntity FROM MyEntity myEntity WHERE myEntity.myEnum = 'A'"
    )
})
public class MyEntity {
    //...
    @Enumerated(STRING)
    private MyEnum myEnum;
    //...
}

枚举非常简单:

public enum MyEnum {
    A, B;
}

在我的主要课程中,我有以下主要功能:

    public static void main(String[] args) {
        /...
        MyEntity myEntity = new MyEntity();
        myEntity.setMyEnum(MyEnum.A);

        tx.begin();
        em.persist(myEntity);
        tx.commit();

        TypedQuery<MyEntity> myEnumEntitiesQuery =
                em.createNamedQuery("MyEntity.findEnum", MyEntity.class);
        List<MyEntity> myEntities = myEnumEntitiesQuery.getResultList();
    }

所以althuogh我有@Enumerated(STRING)注释我仍然有异常:java.lang.String不能转换为java.lang.Enum。有什么想法我得到这个错误?谢谢!

堆栈追踪:

Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-6168] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException
Exception Description: Query failed to prepare, unexpected error occurred: [java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum].
Internal Exception: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum
Query: ReadAllQuery(name="MyEntity.findEnum" referenceClass=MyEntity jpql="SELECT myEntity FROM MyEntity myEntity WHERE myEntity.myEnum = 'ENUM'")
    at org.eclipse.persistence.exceptions.QueryException.prepareFailed(QueryException.java:1590)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:680)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:901)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:613)
    at org.eclipse.persistence.internal.jpa.QueryImpl.getDatabaseQueryInternal(QueryImpl.java:341)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1124)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1144)
    at com.istvanmosonyi.dbtest.Main.main(Main.java:29)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum
    at org.eclipse.persistence.mappings.converters.EnumTypeConverter.convertObjectValueToDataValue(EnumTypeConverter.java:160)
    at org.eclipse.persistence.mappings.foundation.AbstractDirectMapping.getFieldValue(AbstractDirectMapping.java:776)
    at org.eclipse.persistence.internal.expressions.QueryKeyExpression.getFieldValue(QueryKeyExpression.java:420)
    at org.eclipse.persistence.internal.expressions.ConstantExpression.printSQL(ConstantExpression.java:152)
    at org.eclipse.persistence.expressions.ExpressionOperator.printDuo(ExpressionOperator.java:2239)
    at org.eclipse.persistence.internal.expressions.CompoundExpression.printSQL(CompoundExpression.java:286)
    at org.eclipse.persistence.internal.expressions.RelationExpression.printSQL(RelationExpression.java:899)
    at org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter.translateExpression(ExpressionSQLPrinter.java:306)
    at org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter.printExpression(ExpressionSQLPrinter.java:129)
    at org.eclipse.persistence.internal.expressions.SQLSelectStatement.printSQL(SQLSelectStatement.java:1683)
    at org.eclipse.persistence.internal.databaseaccess.DatabasePlatform.printSQLSelectStatement(DatabasePlatform.java:3178)
    at org.eclipse.persistence.platform.database.PostgreSQLPlatform.printSQLSelectStatement(PostgreSQLPlatform.java:530)
    at org.eclipse.persistence.internal.expressions.SQLSelectStatement.buildCall(SQLSelectStatement.java:782)
    at org.eclipse.persistence.internal.expressions.SQLSelectStatement.buildCall(SQLSelectStatement.java:792)
    at org.eclipse.persistence.descriptors.ClassDescriptor.buildCallFromStatement(ClassDescriptor.java:813)
    at org.eclipse.persistence.internal.queries.StatementQueryMechanism.setCallFromStatement(StatementQueryMechanism.java:390)
    at org.eclipse.persistence.internal.queries.StatementQueryMechanism.prepareSelectAllRows(StatementQueryMechanism.java:315)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareSelectAllRows(ExpressionQueryMechanism.java:1721)
    at org.eclipse.persistence.queries.ReadAllQuery.prepareSelectAllRows(ReadAllQuery.java:813)
    at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:744)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:661)
    ... 6 more

1 个答案:

答案 0 :(得分:3)

问题在于您使用以下声明进行了定义

@Enumerated(STRING)
private MyEnum myEnum

该字段的类型为MyEnum类型。但是您将字符串值传递给查询。所以你必须做以下事情(可能还有其他可能做到这一点,但我只知道以下内容):

  1. 使用命名参数:

    定义查询字符串
    @NamedQuery(
        name = "MyEntity.findEnum",
        query = "SELECT me FROM MyEntity me WHERE me.myEnum = :myEnumValue"
    

  2. 创建TypedQuery的实例,并在thiss实例上设置命名参数,如下所示:

    TypedQuery<MyEntity> myEnumEntitiesQuery =
            em.createNamedQuery("MyEntity.findEnum", MyEntity.class);
    myEnumEntitiesQuery.setParameter("myEnumValue", MyEnum.A);
    List<MyEntity> myEntities = myEnumEntitiesQuery.getResultList();