I want to add a list of selected enumeration to one of my JPA entities:
@Entity
public class FooEntity {
...
List<SecurityType> securityTypes;
@ElementCollection(targetClass = SecurityType.class)
public List<SecurityType> getSecurityTypes() {
return securityTypes;
}
public void setSecurityTypes(List<SecurityType> securityTypes) {
this.emum1s = securityTypes;
}
...
}
With the Enum
public enum SecurityType {
FOO, BAR
}
I've added the data model to my DB according to default behaviour of JPA.
Unfortunately I receive an Exception while saving an object of this type to the DB:
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)
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 String into Enum (since the object is already an enum).
I started debugging and found that the auto-generated JPA-proxy object does not contain an attribute List<Enum1>, but List<String> instead:
For me this looks like a bug and I found an issue describing this topic:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=328371
I tried this with default Glassfish 3.1.2.2 (Eclipselink 1.3) and Eclipselink 1.4. Both the same.
Could anyone help, using enums in JPA @ElementCollections? Is this a bug or am I missing something?
Thanks!