问题描述:

I've now wasted several hours trying to get this to work, but Hibernate still keeps me wondering what's going on there inside.

Here's what I want to do:

Simply persist a List of enums via @Enumerated which looks like this:

@LazyCollection(LazyCollectionOption.FALSE)

@ElementCollection(targetClass=Role.class)

@JoinTable(name = "userroles", joinColumns = @JoinColumn(name = "userId"))

@Enumerated(EnumType.ORDINAL)

private List<Role> roles;

My enum class:

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;

}

}

The error I'm encountering occurs at a simple persist() of the class which is holding the List. And it's root cause is:

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]

How could I resolve this? There doesn't seem to be any known bugs of this and I just found non-answered threads in several forums. I also tried EnumType.STRING and leaving some of the annotations, without effect though :/

EDIT:

I also tried to just use a raw enum with standard constructor and no fields, nothing but the values. So the error must not lie in my enum-pojo.

EDIT2:

My problem lied a bit elsewhere. (JSF and type safety). However I will accept the answer how pushed me in the right direction :)

网友答案:

As your Enum have custom attributes you have some options:

  1. Create your custom type (UserType) as follows: https://community.jboss.org/wiki/UserTypeforpersistingaTypesafeEnumerationwithaVARCHARcolumn
  2. Create your Enum property as a transiente and use a String getter and setter to populate.
相关阅读:
Top