JPA's Map query by JPQL failed

前端 未结 3 664
没有蜡笔的小新
没有蜡笔的小新 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.

    0 讨论(0)
  • 2020-12-10 14:43

    I had the same problem. It looks like accessing map by ref (without VALUE()) already gives you a map entry value, i.e. the next JPQL should be transformed to a valid SQL:

    select r from Relation r join r.langMap m where ( KEY(m) = :locale and m = :value )
    
    0 讨论(0)
  • 2020-12-10 14:57

    The correct JPQL could be like this:

    SELECT r FROM Relation r JOIN r.langMap map
    WHERE map[:locale] = :value
    
    0 讨论(0)
提交回复
热议问题