简体   繁体   中英

HQL how to query ElementCollection of String

I have class like

public User{
   Long id;
   Set<String> roles;
}

How do I query all User objects with the role of "ADMIN"

EDIT:

I'm using Hibernate 3.0.5. And have tried most of the obvious approaches.

from Users where roles in('ADMIN') gives a JDBC error. from Users u where u.roles in('ADMIN') gives a class cast exception

I think this may be a problem with this particular version of hibernate.

I've found solution:

"from User as user where 'ADMIN' in elements(user.roles)";

Somehow hql function value() have to help with this, you can also experiment with it, but that hql query above works for me.

您可以使用以下查询

"from User as user where user.id in (select user.id from Role as role left join role.user as user where role.name = 'ADMIN')"

这应该这样做:

session.createQuery("from User where roles in ('ADMIN')");

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