HQL:查询java.util.Map的值

K.C*_*.C. 7 java hibernate jpa hql map

我尝试了这个hql查询,但是当我在以下查询中使用actProp [:key] =:value时,它会抛出UnsupportedOperationException :

选择映射actionProperties中包含值对x,y或z,y的所有操作:

Query query = getSession().createQuery(
    "select a from Action a                     " +
    " join a.actionProperties actProp           " +
    "   where (index(actProp) = :key            " +
    "           and actProp[:key] = :value )    " +
    "     or  (index(actProp) = :key2           " +
    "           and actProp[:key2] = :value )   ");
Run Code Online (Sandbox Code Playgroud)

例外:

java.lang.UnsupportedOperationException
        at org.hibernate.hql.ast.tree.IdentNode.resolveIndex(IdentNode.java:67)
Run Code Online (Sandbox Code Playgroud)

在实体Action中:

@CollectionOfElements(fetch = FetchType.EAGER)
private Map<String, String> actionProperties;
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用Hibernate Criteria,但我不认为这是可能的.

有没有人知道要替换的东西:actProp [:key] =:带有工作代码的

K.C*_*.C. 8

经过一些试验和错误,我终于找到了解决方案,这不是那么简单.

Query query = getSession().createQuery(
        " from Action a " +
        " join a.actionProperties actProp                      " +
        "   where( (index(actProp) = :key                      " +
        "           and :value in elements(a.actionProperties))" +
        "       or (index(actProp) = :key2                     " +
        "           and :value in elements(a.actionProperties)) )"
        );
Run Code Online (Sandbox Code Playgroud)

因此,为了匹配键我使用了index()函数并匹配我使用elements()函数的值.

如果您知道更好的解决方案,请告诉我.

  • 我遇到了同样的问题而且对我来说似乎并不能保证密钥和值实际上是一起关联的.它只是保证在属性中有一个这样的键和这样的值,但是值可以与不适合我的不同键相关联. (2认同)