0

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:

Debugging information of NetBeans

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!

Tim
  • 353
  • 2
  • 8

2 Answers2

0

There is no 1.3 or 1.4 release, did you mean 2.4? The latest release in 2.5, so you may want to try that.

EclipseLink does not auto generate a proxy, so you are looking at your class, as you defined it. Check that you compiled/deployed your code, did it use to be a String?

How did you build the object your are inserting? Check that you are not adding Strings to the collection, it seems that you are.

James
  • 17,965
  • 11
  • 91
  • 146
  • Hi James, thanks for your response. I investigated a lot and tried to tackle this issue down (tried different versions and libraries). In the end it came out that I was struggling with JSF instead of JPA. The problem was that JSF cannot convert Enums automatically (you need a converter): http://stackoverflow.com/questions/5340762/hselectmanylistbox-jsf-and-enums-class-cast-error – Tim Jul 19 '13 at 07:17
0

Besides your @ElementCollection(fetch = FetchType.EAGER) annotation use an @Enumerated(EnumType.String) along with a @CollectionTable (pointing to the name of the mapping table and containing a joinColumn) and an @Column (specifying the name of the SecurityType column in the table for your FooEntity)

Simeon Leyzerzon
  • 18,658
  • 9
  • 54
  • 82