我试过这个 hql 查询,但是当我在以下查询中使用 actProp[:key] = :value 时它会抛出 UnsupportedOperationException:
选择 map actionProperties中包含值对x,y或z,y的所有 Action :
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 ) ");
异常:
java.lang.UnsupportedOperationException
at org.hibernate.hql.ast.tree.IdentNode.resolveIndex(IdentNode.java:67)
在实体Action中:
@CollectionOfElements(fetch = FetchType.EAGER)
private Map<String, String> actionProperties;
我也尝试为此使用 Hibernate Criteria,但我认为这是不可能的。
有人知道用工作代码替换 : actProp[:key] = :value 吗?
最佳答案
经过反复试验,我终于找到了解决方案,但不是那么简单。
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)) )"
);
因此,为了匹配键,我使用了 index()
函数,为了匹配值,我使用了 elements()
函数。
如果您知道更好的解决方案,请告诉我。
关于java - HQL:查询java.util.Map的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14896549/