读书人

当中表的 in 子查询

发布时间: 2012-09-06 10:37:01 作者: rapoo

中间表的 in 子查询
role 表
loginuser表 他们是多对多的关系

还有dept表 和loginuser是一对多的关系

我想要查询 某些部门下 的某些角色下的 所有用户

在数据库中的查询语句是这样
select * from login_user u , dept d on d.did=u.did and d.did in (1,2) and u.isteacher=1 and u.userid in (select userid from user_role ur where ur.roleid in (1,2))

但是在 hql中 则会报错
String hql="select u from LoginUser u , Dept d where d.did=u.dept.did and d.did in (1,2) and u.userid in (select u2.userid from LoginUser u2 , u2.roles r where r.roleid in ("+ roleids+") ";

报错:
00:38:37,464 ERROR org.hibernate.hql.PARSER:56 - line 1:225: expecting CLOSE, found 'null'

开始以为hql不支持from 后的 in 子查询
或者不支持多对多关系的子查询

后来一查, 对于多对多里面的 set集合可以用 u in elements(r.loginUsers)
来取。
修改后的sql语句
String hql="select u from Dept d, Role r ,LoginUser u where d.did=u.dept.did and d.did in ("+deptids+") and u in elements(r.loginUsers) and r.roleid in("+roleids+")";



读书人网 >软件架构设计

热点推荐