HQL:查询java.util.Map的值

时间:2022-03-17 19:36:54

I tried this hql query, but it throws an UnsupportedOperationException when I use actProp[:key] = :value in the following query:

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

Select all the actions that contain the value pairs x,y or z,y in the map actionProperties:

选择映射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 )   ");

The exception:

java.lang.UnsupportedOperationException
        at org.hibernate.hql.ast.tree.IdentNode.resolveIndex(IdentNode.java:67)

In the entity Action:

在实体Action中:

@CollectionOfElements(fetch = FetchType.EAGER)
private Map<String, String> actionProperties;

I also tried to use Hibernate Criteria for this, but I don't think this it is possible.

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

Does anybody know something to replace : actProp[:key] = :value with working code?

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

1 个解决方案

#1


8  

After some trial and error I finally found the solution, which is not so straightforward.

经过一些反复试验后,我终于找到了解决方案,这并不是那么简单。

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)) )"
        );

So, for matching on the key I used the index() function and for matching on the value I used the elements() function.

因此,对于键上的匹配,我使用了index()函数,并且对于我使用了elements()函数的值进行匹配。

If you know a better solution, let me know.

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

#1


8  

After some trial and error I finally found the solution, which is not so straightforward.

经过一些反复试验后,我终于找到了解决方案,这并不是那么简单。

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)) )"
        );

So, for matching on the key I used the index() function and for matching on the value I used the elements() function.

因此,对于键上的匹配,我使用了index()函数,并且对于我使用了elements()函数的值进行匹配。

If you know a better solution, let me know.

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