开发者

Selecting value using HQL based on a key of a Map

Suppose I have following JPA mapping.



    @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. 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 pa开发者_开发知识库rticular key-value pair in additionalInfo map. 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). See section 14.10. Expressions of the Hibernate reference documentation

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜