4

I'm looking for JPA 2 criteria equivalent of this JPQL query :

select e from Entity e join e.myMap m where KEY(m) = 'myKey' and VALUE(m) = ‘myValue’ 

Thank you !

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Albataur
  • 97
  • 1
  • 2
  • 5
  • Take a look at this answer: http://stackoverflow.com/questions/6246735/hibernate-jpa2-h2-querying-elementcollections-hashmap-by-key – nowaq Dec 13 '11 at 14:59

1 Answers1

6

Not tested, but I guess this should be OK:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Entity> criteria = cb.createQuery(Entity.class);
Root<Entity> entity = criteria.from(Entity.class);
MapJoin<Entity, String, String> mapJoin = entity.joinMap(Entity_.myMap);
criteria.where(cb.and(cb.equal(mapJoin.key(), "myKey"),
                      cb.equal(mapJoin.value(), "myValue")));
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    Thank you very much for your answer, it compiles ! Unfortunately I get this bug now : http://stackoverflow.com/questions/5580614/jpa-criteriabuilder-join-maps ... – Albataur Dec 13 '11 at 15:20