在保存具有枚举字段的可嵌入类的实体时获取ClassCastException

时间:2012-01-17 19:00:47

标签: jpa enumeration classcastexception embeddable

服务器:JBoss6 技术:JPA 2,EJB 3.1

我有一个实体,它有一个可嵌入的类,它封装了一组共同​​的字段,可以在同一个实体上多次使用,如下所示:

@Entity
@Table(name = "form_x")
public class FormX extends SurveyForm implements Serializable {

    private static final long serialVersionUID = 1321874423212L;

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name="data", column = @Column(name = "field01")),
        @AttributeOverride(name="metadata", column = @Column(name = "field01_metadata")),
        @AttributeOverride(name="metadataComment", column = @Column(name = "field01_metadata_comment"))
    })
    private SingleSelectionQuestion<Agreement> field1;

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name="data", column = @Column(name = "field02")),
        @AttributeOverride(name="metadata", column = @Column(name = "field02_metadata")),
        @AttributeOverride(name="metadataComment", column = @Column(name = "field02_metadata_comment"))
    })
    private SingleSelectionQuestion<Frequency> field2;

    ...getters and setters...
}

@Embeddable
public class SingleSelectionQuestion<T> implements Serializable {

    private static final long serialVersionUID = -7389172046941228140L;

    @Enumerated(EnumType.STRING)
    private T data;

    @Enumerated(EnumType.STRING)
    private QuestionMetaData metadata;

    private String metadataComment;

    ...getters and setters...
}

如果协议和频率是枚举。

但是当我尝试保存这个表单(entityManage#merge在一个已经存在的实体上)时,我得到一个ClassCastException,如下面的stacktrace中所示:

Caused by: javax.transaction.RollbackException: ARJUNA-16053 Could not commit transaction.
    at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1170) [:6.0.0.Final]
    at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:119) [:6.0.0.Final]
    at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:75) [:6.0.0.Final]
    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.endTransaction(CMTTxInterceptor.java:82) [:0.0.1]
    ... 110 more
Caused by: javax.persistence.PersistenceException: error during managed flush
    at org.hibernate.ejb.AbstractEntityManagerImpl$3.mapManagedFlushFailure(AbstractEntityManagerImpl.java:1072) [:3.6.0.Final]
    at org.hibernate.transaction.synchronization.CallbackCoordinator.beforeCompletion(CallbackCoordinator.java:122) [:3.6.0.Final]
    at org.hibernate.transaction.synchronization.HibernateSynchronizationImpl.beforeCompletion(HibernateSynchronizationImpl.java:51) [:3.6.0.Final]
    at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:97) [:6.0.0.Final]
    at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:274) [:6.0.0.Final]
    at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:94) [:6.0.0.Final]
    at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:159) [:6.0.0.Final]
    at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1158) [:6.0.0.Final]
    ... 113 more
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum
    at org.hibernate.type.EnumType.nullSafeSet(EnumType.java:150) [:3.6.0.Final]
    at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:146) [:3.6.0.Final]
    at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:336) [:3.6.0.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2166) [:3.6.0.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2540) [:3.6.0.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2476) [:3.6.0.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2803) [:3.6.0.Final]
    at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:113) [:3.6.0.Final]
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273) [:3.6.0.Final]
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265) [:3.6.0.Final]
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185) [:3.6.0.Final]
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) [:3.6.0.Final]
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) [:3.6.0.Final]
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216) [:3.6.0.Final]
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383) [:3.6.0.Final]
    at org.hibernate.transaction.synchronization.CallbackCoordinator.beforeCompletion(CallbackCoordinator.java:117) [:3.6.0.Final]
    ... 119 more

为什么即使在SingleSelectionQuestion类的字段上使用@Enumerated注释也会出现此错误?

任何人都知道我做错了什么?

感谢。

0 个答案:

没有答案