Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QueryDsl - filtering Map based on key

I have folowing property in my hibenrate entity class:

@MapKeyJoinColumn(name = "language_code")
@LazyCollection(LazyCollectionOption.EXTRA)
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "text_translations", joinColumns = @JoinColumn(name = "text_id"))
private Map<Language, String> translations = new HashMap<>();

Now I want to query this entity and filter content of map by user's language (i.e. by map's key). I have folowing join in my query:

StringPath titleTran = new StringPath("title_tran");
from(entity).
.leftJoin(entity.translations, titleTran).fetch().where(?mapKey?.eq(userLanguage));

What I need is ?mapKey? to by path to language of titleTran path. Is this somehow possible in QueryDsl?

like image 319
Richard Kakaš Avatar asked Feb 10 '15 11:02

Richard Kakaš


1 Answers

If you want to search for a given Map key, you can use the following HQL query:

select me
from MyEntity me
join me.translations tr
where
    index(tr) = :lang

or with JPQL:

select me
from MyEntity me
join me.translations tr
where
    key(tr) = :lang
like image 121
Vlad Mihalcea Avatar answered Sep 28 '22 04:09

Vlad Mihalcea