Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » ElementCollection Map and CriteriaQuery
icon4.gif  ElementCollection Map and CriteriaQuery [message #1065471] Tue, 25 June 2013 20:28 Go to next message
Raffael Bachmann is currently offline Raffael BachmannFriend
Messages: 4
Registered: June 2013
Junior Member

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:

public class TestEntity {
    public enum MyEnum {

    @GeneratedValue(strategy = GenerationType.TABLE, generator = "pkGen")
    @TableGenerator(allocationSize = 1000, initialValue = 0, name = "pkGen", table = "PRIMARY_KEYS")
    private int id;

    @ElementCollection(targetClass = MyEnum.class)
    Map<String, MyEnum> col = new HashMap<>();

    public Map<String, MyEnum> getCol() {
        return col;

And the querry code:

public class EclipseLinkBean {

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"));

    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:


java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum
at org.eclipse.persistence.mappings.converters.EnumTypeConverter.convertObjectValueToDataValue( ~[org.eclipse.persistence.core_2.5.0.v20130507-3faac2b.jar:na]
at org.eclipse.persistence.mappings.DirectCollectionMapping.getFieldValue( ~[org.eclipse.persistence.core_2.5.0.v20130507-3faac2b.jar:na]
at org.eclipse.persistence.internal.expressions.QueryKeyExpression.getFieldValue( ~[org.eclipse.persistence.core_2.5.0.v20130507-3faac2b.jar:na]
at org.eclipse.persistence.internal.expressions.ParameterExpression.getValue( ~[org.eclipse.persistence.core_2.5.0.v20130507-3faac2b.jar:na]
at org.eclipse.persistence.internal.databaseaccess.DatabaseCall.translate( ~[org.eclipse.persistence.core_2.5.0.v20130507-3faac2b.jar:na]

It works if I use an Integer instead of an enum like this:
	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?


Re: ElementCollection Map and CriteriaQuery [message #1066624 is a reply to message #1065471] Wed, 03 July 2013 13:34 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Yes, it seems like a bug, please log a bug and vote for it,

You may also consider making the enum into a class/Entity and using a OneToMany.

James : Wiki : Book : Blog : Twitter
Previous Topic:Mapping Oracle NUMBER type to Java Type Options
Next Topic:EclipseLink RestService entityManager is null
Goto Forum:

Current Time: Fri May 25 09:11:51 GMT 2018

Powered by FUDForum. Page generated in 0.01959 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top