7

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

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

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:

@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.

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

1 Answer 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.

If you know a better solution, let me know.

1
  • 2
    I got a same problem and to me it seems this does not guarantee that the key and value are actually associated together. It just guarantees there is a such a key and such a value in the properties but the value could be associated with a different key which does not work for me.
    – Thang Mai
    Jul 14, 2017 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.