简体   繁体   English

HQL如何查询String的ElementCollection

[英]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" 如何查询角色为"ADMIN"所有User对象

EDIT: 编辑:

I'm using Hibernate 3.0.5. 我正在使用Hibernate 3.0.5。 And have tried most of the obvious approaches. 并尝试了大多数明显的方法。

from Users where roles in('ADMIN') gives a JDBC error. from Users where roles in('ADMIN')给出JDBC错误的用户。 from Users u where u.roles in('ADMIN') gives a class cast exception from Users u where u.roles in('ADMIN')给出了类from Users u where u.roles in('ADMIN')异常

I think this may be a problem with this particular version of hibernate. 我认为这可能是这个特定版本的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. 不知怎的,hql函数value()必须帮助解决这个问题,你也可以尝试一下,但上面的hql查询对我有用。

您可以使用以下查询

"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')");

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

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