QueryDsl - key-based map filtering

I have the following property in the 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 object and filter the contents of the map by the user's language (i.e. by the map key). I want to join my request:

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

What I need? mapKey? on the language path of the titleTran path. Is this possible in QueryDsl?

+5
source share
2 answers

If you want to find the 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 
+3
source

In Querydsl JPA, the following works

 query.from(me).where(me.translations.containsKey(lang)).list(me); 
0
source

Source: https://habr.com/ru/post/1213043/


All Articles