java - HQL:查询java.util.Map的值

标签 java hibernate jpa map hql

我试过这个 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/

相关文章:

java - JPA 中多列的不同

java - 以 Parent 和 Child 类作为参数的方法重载

java - 对 4GB JVM 和 3GB 缓存进行适当的 JVM/GC 调优

hibernate - 在Hibernate中映射本地化的字符串-有什么最佳实践吗?

java - InheritanceType.JOINED 策略不适用于 Spring Boot

java - 无法在 spring data jpa 的 findAll 方法中使用规范

java - 链接方法模式

java - Kaltura 文件上传 API

java - 如何将表与该表的实体类中 View 的一列连接起来

java - 在Hibernate中创建自动递增的数据库字段