HQL how to query ElementCollection of String

拜拜、爱过 提交于 2019-12-18 13:30:04

问题


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.


回答1:


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.




回答2:


You can use the query below

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



回答3:


This should do it:

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


来源:https://stackoverflow.com/questions/14090364/hql-how-to-query-elementcollection-of-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!