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 Bachmann
Messages: 2
Registered: June 2013
Junior Member
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
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 Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

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

http://wiki.eclipse.org/EclipseLink/Bugs

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: Sat Oct 25 07:07:37 GMT 2014

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

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