简体   繁体   中英

Using HQL to search in a Map

I have a Java class (let's call it BigClass) which has a Map as attribute (Map< MyClass, Integer >), what I would like to do is to make an HQL query to filter this classes depending on the values of this map.

In fact, what I would like to do is to recover all instances (b) of BigClass which satisfy for a specific instance (m) of MyClass b.map.get(m) > 0.

For example: I would like to recover an instance of BigClass, if its map has an entry like < m, 2 >, and I would't like to recover an instance of BigClass if its map has an entry like < m, 0 > or if m doesn't belong to the map keys.

I've been searching and I found this post related to my problem:

Using HQL to query on a Map's Values

Although, it didn't give me a solution.


These are some queries I've been trying with no success:

org.hibernate.QueryException: No index value! 
[SELECT e FROM BigClass e WHERE ( map[(SELECT b FROM MyClass b WHERE b.id = ?)] > 0) ]

org.hibernate.QueryException: No index value! 
[SELECT e FROM BigClass e WHERE ( index(map) = (SELECT b FROM MyClass b WHERE b.id = ?) ) > 0) ]

Thanks in advance...

if criteria is an option this might work

session.createCriteria(BigClass.class)
    .joinAlias("map", "entry")
    .add(Restrictions.eq("entry", session.load(MyClass.class, id))
    .list();

Thanks for the answers. I've already solved this by removing the SELECT between the brackets, it was forbidden!

I just added the object as a parameter of the query, and this worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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