Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 328371

Summary: Function KEY() breaks the query if the value of a Map-mapping has a converter
Product: z_Archived Reporter: Igor Mukhin <iimuhin>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: NEW --- QA Contact:
Severity: major    
Priority: P2 CC: iimuhin, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Attachments:
Description Flags
rough proposed fix none

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