Albataur
Albataur

Reputation: 97

Querying a Map using JPA 2.0 criteria

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 !

Upvotes: 4

Views: 6356

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

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")));

Upvotes: 6

Related Questions