Hi,
First I'd like to say sorry for cross-posting. I already posted this problem on stackoverflow (*1), but I have the feeling that i might have hit a bug. And before filing it I'd like to ask here if it is really a bug.
So this is my Problem. When I try to query for a key in an ElementCollection Map<String, MyEnum>.
Here is the simple entity class:
@Entity
public class TestEntity {
public enum MyEnum {
FOO,
BAR;
}
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "pkGen")
@TableGenerator(allocationSize = 1000, initialValue = 0, name = "pkGen", table = "PRIMARY_KEYS")
private int id;
@ElementCollection(targetClass = MyEnum.class)
@Enumerated(EnumType.STRING)
@MapKeyClass(String.class)
Map<String, MyEnum> col = new HashMap<>();
public Map<String, MyEnum> getCol() {
return col;
}
}
And the querry code:
@LocalBean
public class EclipseLinkBean {
@PersistenceContext
private EntityManager em;
public void test() {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<TestEntity> q = builder.createQuery(TestEntity.class);
Root<TestEntity> testEntity = q.from(TestEntity.class);
MapJoin<TestEntity, String, MyEnum> col = testEntity.joinMap("col");
q.where(builder.equal(col.key(), "foobar"));
em.createQuery(q).getResultList();
}
public void test2() {
em.createQuery("SELECT te FROM TestEntity te JOIN te.col c WHERE KEY(c) = 'foobar'").getResultList();
}
}
It doesn't matter if i use test() or test2() the exception is always this:
Quote:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum
at org.eclipse.persistence.mappings.converters.EnumTypeConverter.convertObjectValueToDataValue(EnumTypeConverter.java:160) ~[org.eclipse.persistence.core_2.5.0.v20130507-3faac2b.jar:na]
at org.eclipse.persistence.mappings.DirectCollectionMapping.getFieldValue(DirectCollectionMapping.java:2250) ~[org.eclipse.persistence.core_2.5.0.v20130507-3faac2b.jar:na]
at org.eclipse.persistence.internal.expressions.QueryKeyExpression.getFieldValue(QueryKeyExpression.java:396) ~[org.eclipse.persistence.core_2.5.0.v20130507-3faac2b.jar:na]
at org.eclipse.persistence.internal.expressions.ParameterExpression.getValue(ParameterExpression.java:285) ~[org.eclipse.persistence.core_2.5.0.v20130507-3faac2b.jar:na]
at org.eclipse.persistence.internal.databaseaccess.DatabaseCall.translate(DatabaseCall.java:1102) ~[org.eclipse.persistence.core_2.5.0.v20130507-3faac2b.jar:na]
...
It works if I use an Integer instead of an enum like this:
@ElementCollection()
Map<String, Integer> col = new HashMap<>();
but then I'd have to convert it myself to the enum.
I tried with version 2.4.1 and 2.5
Is this a bug and if yes should i write a bug report?
Or what am i doing wrong?
Thanks
Raffael
*1 stackoverflow.com/questions/17301603/how-to-query-for-the-key-in-a-jpa-elementcollection-map-having-an-enum-as-value