简体   繁体   English

使用基于地图键的HQL选择值

[英]Selecting value using HQL based on a key of a Map

Suppose I have following JPA mapping. 假设我有以下JPA映射。



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

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


    }

I want to write HQL to fetch all the transactions which has particular key-value pair in the additionalInfo map. 我想写HQL来获取所有在AdditionalInfo映射中具有特定键值对的交易。 I think I have to do a join like follows, 我想我必须像下面这样加入



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

But I'm not clear how to put the WHERE clause to get match with particular key-value pair in additionalInfo map. 但是我不清楚如何在附加信息映射中放置WHERE子句以与特定键值对匹配。 Can anybody help me on this? 有人可以帮我吗?

Thanks in advance. 提前致谢。

You need to use HQL index() specific function, that applies to aliases of a joined indexed collection (arrays, lists, and maps). 您需要使用特定于HQL index()函数,该函数适用于联接的索引集合(数组,列表和映射)的别名。 See section 14.10. 请参阅第14.10 Expressions of the Hibernate reference documentation Hibernate参考文档的表达式

//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' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM