HQL: requesting value java.util.Map

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

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

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

An exception:

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

In essence, Action:

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

I also tried using the Hibernate criteria for this, but I don't think it is possible.

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

+7
source share
1 answer

After some trial and error, I finally found a solution, which is not so simple.

 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 key matching, I used the index() function, and for matching the value, I used the elements() function.

If you know the best solution, let me know.

+8
source

All Articles