title: ClassCastException when persisting enum via Hibernate
body: <p>I've now wasted several hours trying to get this to work, but Hibernate still keeps me wondering what's going on there inside.<br>
  Here's what I want to do:<br>
  Simply persist a <code>List</code> of <code>enum</code>s via <code>@Enumerated</code> which looks like this:</p>
  
  <pre><code>@LazyCollection(LazyCollectionOption.FALSE)
  @ElementCollection(targetClass=Role.class)
  @JoinTable(name = "userroles", joinColumns = @JoinColumn(name = "userId"))
  @Enumerated(EnumType.ORDINAL)
  private List&lt;Role&gt; roles;
  </code></pre>
  
  <p>My <code>enum</code> class:</p>
  
  <pre><code>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;
  }   
  }
  </code></pre>
  
  <p>The error I'm encountering occurs at a simple <code>persist()</code> of the class which is holding the <code>List</code>. And it's root cause is:</p>
  
  <pre><code>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]
  </code></pre>
  
  <p>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 <code>EnumType.STRING</code> and leaving some of the annotations, without effect though :/<br>
  <strong>EDIT:</strong>
  I also tried to just use a raw <code>enum</code> with standard constructor and no fields, nothing but the values. So the error must not lie in my enum-pojo.<br>
  <strong>EDIT2:</strong><br>
  My problem lied a bit elsewhere. (<a href="https://stackoverflow.com/questions/16433250/jsf-and-type-safety">JSF and type safety</a>). However I will accept the answer how pushed me in the right direction :)</p>