Bug 328371 - Function KEY() breaks the query if the value of a Map-mapping has a converter
Summary: Function KEY() breaks the query if the value of a Map-mapping has a converter
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 7
: P2 major with 3 votes (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-10-21 11:13 EDT by Igor Mukhin CLA
Modified: 2022-06-09 10:22 EDT (History)
2 users (show)

See Also:


Attachments
rough proposed fix (4.08 KB, patch)
2011-05-09 15:13 EDT, Chris Delahunt CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Igor Mukhin CLA 2010-10-21 11:13:37 EDT
Build Identifier: 2.1.0

@Entity
public class Person {
	public enum Enum1 { VALUE1, VALUE2 }

	@Id
	private int id;

	@ElementCollection
	private Map<String, Integer> str2int = new Hashtable<String, Integer>();

	@ElementCollection
	private Map<String, Enum1> str2enum = new Hashtable<String, Enum1>();
}

Next query will work (because no converter needed for Integer):
TypedQuery<Person> q = em.createQuery("select p from Person p JOIN p.str2int me where key(me) = ?1", Person.class);
q.setParameter(1, "test");
List<Person> persons = q.getResultList();


This query will fail (because EL will incorrectly use the value converter (enum) for the key (string)):
TypedQuery<Person> q = em.createQuery("select p from Person p JOIN p.str2enum me where key(me) = ?1", Person.class);
q.setParameter(1, "test");
List<Person> persons = q.getResultList();

Exception: 
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum
	at org.eclipse.persistence.mappings.converters.EnumTypeConverter.convertObjectValueToDataValue(EnumTypeConverter.java:137)
	at org.eclipse.persistence.mappings.DirectCollectionMapping.getFieldValue(DirectCollectionMapping.java:2132)
	at org.eclipse.persistence.internal.expressions.QueryKeyExpression.getFieldValue(QueryKeyExpression.java:374)
	at org.eclipse.persistence.internal.expressions.ParameterExpression.getValue(ParameterExpression.java:256)


Possible solution: the converter for the key-type should be used with KEY() function.

Thanks.

Reproducible: Always

Steps to Reproduce:
see description
Comment 1 Tom Ware CLA 2010-11-01 11:34:26 EDT
Setting target and priority.  See the following page for details about these
fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines
Comment 2 Chris Delahunt CLA 2011-05-09 15:13:09 EDT
Created attachment 195130 [details]
rough proposed fix

a rough fix I came across while working on something else for the Extensible mappings feature.  Not ready for checkin because I do not have a test case.
Comment 3 Eclipse Webmaster CLA 2022-06-09 10:22:12 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink