简体   繁体   English

HQL:查询java.util.Map的值

[英]HQL: querying value of java.util.Map

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] =:带有工作代码的

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. 如果您知道更好的解决方案,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM