【问题标题】:Selecting value using HQL based on a key of a Map基于 Map 的键使用 HQL 选择值
【发布时间】:2011-08-18 01:10:24
【问题描述】:

假设我有以下 JPA 映射。



    @Entity
    @Table(name = "transaction")
    public class Transaction {

        @ElementCollection(targetClass = String.class, fetch = FetchType.EAGER)
        @JoinTable(name = "trx_addi_info")
        private Map additionalInfo;


    }

我想编写 HQL 来获取在附加信息映射中具有特定键值对的所有事务。我想我必须像下面这样加入,



    SELECT trx FROM Transaction trx inner join trx.additionalInfo addInfo WHERE addInfo.????

但我不清楚如何放置 WHERE 子句以匹配附加信息映射中的特定键值对。有人可以帮我解决这个问题吗?

提前致谢。

【问题讨论】:

    标签: java hibernate hibernate-mapping


    【解决方案1】:

    您需要使用 HQL index() 特定函数,该函数适用于连接索引集合(数组、列表和映射)的别名。请参阅 Hibernate 参考文档的14.10. Expressions 部分

    //Example of HQL returning `Transaction` object that have `additianlInfo` with   
    //the `KEY` equal to the string `test`
    
    SELECT trx FROM Transaction trx inner join trx.additionalInfo addInfo WHERE index(addInfo) > 'test' 
    

    【讨论】:

      猜你喜欢
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 2015-04-01
      • 1970-01-01
      • 2016-11-02
      • 2017-01-22
      • 2012-11-29
      相关资源
      最近更新 更多