2

I am trying a typedQuery that is giving me the following error: java.lang.IllegalArgumentException: Cannot create element join for a collection of non-entities!

I am using JPA 2.0

My code:

@Entity
public class Example {
    @ElementCollection(targetClass=ExampleData .class,fetch = FetchType.EAGER)
@MapKeyColumn(name = "locale_key")
@CollectionTable(name = "exampleLocalizedData", joinColumns = @JoinColumn(name = "exampleId"))
     private Map<String, IExampleData> exampleLocalizedData;
}

@Embeddable
public class ExampleData {
     private String data;
     .....
} 

My query :

String querySentence = "SELECT DISTINCT e FROM Example e WHERE e.exampleLocalizedData[:locale].name LIKE :filter ";
        TypedQuery<Example > actualQuery = entityManager.createQuery(querySentence, Example .class);

My question is, which is the correct query for doing this?

Regards.

edit:

final query if any wants to know :

select DISTINCT e FROM Example e join e.exampleLocalizedData r WHERE index(r) = :locale AND r.name LIKE :filter .
pata
  • 959
  • 2
  • 18
  • 35
  • Try to use explicit `JOIN`: `String querySentence = "SELECT DISTINCT e FROM Example e JOIN e.exampleLocalizedData eld WHERE eld[:locale].name LIKE :filter "; TypedQuery actualQuery = entityManager.createQuery(querySentence, Example .class);` – n1ckolas Feb 26 '13 at 13:29
  • that gives me a java.lang.UnsupportedOperationException – pata Feb 26 '13 at 13:44
  • Another shot (other's omitted): `FROM Example e JOIN e.exampleLocalizedData[:locale] eld WHERE eld.name LIKE :filter` – n1ckolas Feb 26 '13 at 14:01
  • n1ckolas: thats gives org.hibernate.hql.ast.QuerySyntaxException: unexpected token: with the [:locale] – pata Feb 26 '13 at 14:08
  • Sorry for wild guess before, I believed that the cause was in JOIN structure. Nevertheless, I found something which may be useful for you http://stackoverflow.com/a/9311242/655756 – n1ckolas Feb 26 '13 at 14:22
  • great link. if u put is as an answer i will accept it :D – pata Feb 26 '13 at 14:32
  • Added. I don't want to take other's answer, but nevertheless issue was slightly described :) – n1ckolas Feb 26 '13 at 14:40

1 Answers1

0

The issue is in the try of usage map-valued field as ordinary entity.

For this you need to use JPA 2 KEY() and VALUE() functions.

More information you may find here: https://stackoverflow.com/a/9311242/655756

Community
  • 1
  • 1
n1ckolas
  • 4,380
  • 3
  • 37
  • 43