[英]HQL: querying value of java.util.Map
我尝试了这个hql查询,但是当我在以下查询中使用actProp [:key] =:value时,它会抛出UnsupportedOperationException:
选择映射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 ) ");
例外:
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] =:带有工作代码的值 ?
经过一些试验和错误,我终于找到了解决方案,这不是那么简单。
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()
函数。
如果您知道更好的解决方案,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.