Creating new object from Map in Entity

Moderator: admin

Post Reply
tomash
Posts: 1
Joined: Sun Aug 31, 2014 2:20 pm
Contact:

Creating new object from Map in Entity

Post by tomash »

Hi,

It's my first post here and I'm not sure if it's a correct forum to ask... if not, please forgive me...

I have found a problem I cannot solve by myself.

I've created an Entity - Employee that has Map<String, String> phoneNumbers

Code: Select all

//...
@ElementCollection
@CollectionTable(name="EMP_PHONE")
@MapKeyColumn(name="PHONE_TYPE")
@Column(name="PHONE_NUM")
private Map<String, String> phoneNumbers;
//...
I want to get a List of all phoneNumbers as a List of entities.Phone objects having constructor Phone(String type, String number) (and some other constructors as well)

I'm trying with JPQL query:

Code: Select all

Query q = em.createQuery("Select NEW entities.Phone(VALUE(p), KEY(p)) from Employee e JOIN e.phoneNumbers p");
Execution gives me an Exception:
Local Exception Stack:
Exception [EclipseLink-6168] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.QueryException
Exception Description: Query failed to prepare, unexpected error occurred: [java.lang.ClassCastException: org.eclipse.persistence.mappings.DirectToFieldMapping cannot be cast to
org.eclipse.persistence.mappings.CollectionMapping].
Internal Exception: java.lang.ClassCastException: org.eclipse.persistence.mappings.DirectToFieldMapping cannot be cast to org.eclipse.persistence.mappings.CollectionMapping
I have tried to:
  • * not use construct expression - works OK, got a List of Object[]
    * use construct with VALUE(p) and other Sstring - works OK, got a List of Phone
    * use construct with KEY(p) and other String - not working
I haven't try:
  • *Use different provider - is it worth trying?
    *Use Queries - to be done soon


Could anyone give me a hint how to get a list of new objects created from map key and map value?

Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests