title: JPA (Eclipselink) ElementCollection with Enum
body: <p>I want to add a list of selected enumeration to one of my JPA entities:</p>
  
  <pre><code>@Entity
  public class FooEntity {
      ...
      List&lt;SecurityType&gt; securityTypes;
  
      @ElementCollection(targetClass = SecurityType.class)
      public List&lt;SecurityType&gt; getSecurityTypes() {
          return securityTypes;
      }
  
      public void setSecurityTypes(List&lt;SecurityType&gt; securityTypes) {
          this.emum1s = securityTypes;
      }
      ...
  }
  </code></pre>
  
  <p>With the Enum</p>
  
  <pre><code>public enum SecurityType {
      FOO, BAR
  }
  </code></pre>
  
  <p>I've added the data model to my DB according to default behaviour of JPA.</p>
  
  <p>Unfortunately I receive an Exception while saving an object of this type to the DB:</p>
  
  <pre><code>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.DirectCollectionMapping.postInsert(DirectCollectionMapping.java:2209)
  at org.eclipse.persistence.descriptors.DescriptorQueryManager.postInsert(DescriptorQueryManager.java:980)
  at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:480)
  </code></pre>
  
  <p>As you can see, the exception comes from JPA. I was a bit surprised, since I do have a object of enum type "SecurityType" which is written to the data base as Integer (default behaviour) which should not end in a cast of <strong>String</strong> into <strong>Enum</strong> (since the object is already an enum).</p>
  
  <p>I started debugging and found that the auto-generated JPA-proxy object does not contain an attribute <strong>List&lt;Enum1&gt;</strong>, but <strong>List&lt;String&gt;</strong> instead:</p>
  
  <p><img src="https://i.stack.imgur.com/vAJjr.jpg" alt="Debugging information of NetBeans"></p>
  
  <p>For me this looks like a bug and I found an issue describing this topic:</p>
  
  <p><a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=328371" rel="nofollow noreferrer">https://bugs.eclipse.org/bugs/show_bug.cgi?id=328371</a></p>
  
  <p>I tried this with default Glassfish 3.1.2.2 (Eclipselink 1.3) and Eclipselink 1.4. Both the same.</p>
  
  <p>Could anyone help, using enums in JPA @ElementCollections? Is this a bug or am I missing something?</p>
  
  <p>Thanks!</p>