有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

HibernateHQL:查询java的值。util。地图

我尝试了此hql查询,但当我在以下查询中使用actProp[:key]=:value时,它抛出了一个不支持的操作异常:

在map 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)

在实体行动中:

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

我也尝试过使用Hibernate标准,但我认为这是不可能的

有人知道用工作代码替换:actProp[:key]=:value


共 (1) 个答案

  1. # 1 楼答案

    经过一番尝试和错误,我终于找到了解决办法,这不是那么简单

    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()函数

    如果你知道更好的解决方案,请告诉我