簡體   English   中英

HQL:查詢java.util.Map的值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM