我现在已经浪费了几个小时试图让它工作,但 Hibernate 仍然让我想知道里面发生了什么。
这就是我想要做的:
只需坚持 a List
of enum
s via @Enumerated
,它看起来像这样:
@LazyCollection(LazyCollectionOption.FALSE)
@ElementCollection(targetClass=Role.class)
@JoinTable(name = "userroles", joinColumns = @JoinColumn(name = "userId"))
@Enumerated(EnumType.ORDINAL)
private List<Role> roles;
我的enum
班级:
public enum Role implements Serializable {
employee("Mitarbeiter"),
manager("Geschäftsleitung"),
stationleader("Stationsleitung"),
administration("Verwaltung"),
accountant("Buchhaltung");
@Transient
private String description;
private Role()
{
}
public void setDescription(String description) {
this.description = description;
}
private Role(String desc) {
this.description = desc;
}
@Override
public String toString() {
return description;
}
public String getDescription() {
return description;
}
}
我遇到的错误发生在persist()
持有List
. 它的根本原因是:
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum
at org.hibernate.type.EnumType.nullSafeSet(EnumType.java:118) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:155) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:811) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1201) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:58) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:272) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:264) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:190) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1081) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:315) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:104) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
我该如何解决这个问题?似乎没有任何已知的错误,我只是在几个论坛中发现了未回答的线程。我也尝试EnumType.STRING
留下一些注释,但没有效果:/
编辑:
我还尝试只使用enum
带有标准构造函数且没有字段的原始值,只有值。所以错误一定不在于我的枚举pojo。
EDIT2:
我的问题出在其他地方。(JSF 和类型安全)。但是,我会接受如何将我推向正确方向的答案:)