JPA's Map query by JPQL failed

前端 未结 3 678
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 14:12

I am storing a Map in JPA , which stores a keyword translation in each language . such as one object stores Locale.ENGLISH -> \"Father\" , Locale.CHINESE -> \"Pa

3条回答
  •  余生分开走
    2020-12-10 14:34

    I had a similar problem using the JPQL VALUE() operator with Hibernate. It seems that Hibernate implements the VALUE() operator like the java.util.Map.values() method in Java. It generates a subquery that returns all values in the map, i.e. all rows of the mapping table that are related to the entity holding the Map attribute. As soon as you have more then one key/value pair in the map, a comparison expression, which expects scalar expressions as operands, will fail.

    What you can do is to turn the comparison expression around and convert it to an IN expression.

    Instead of:

    select r from Relation r join r.langMap m 
      where ( KEY(m) = :locale and VALUE(m) = :value )
    

    You can write:

    select r from Relation r join r.langMap m 
      where ( KEY(m) = :locale and :value in (VALUE(m)) )
    

    I hope this way your query will work.

提交回复
热议问题