判断多对多时候中是否存在该对象语句

来源:互联网 发布:西安软件公寓a区 编辑:程序博客网 时间:2024/05/23 01:57
public List<User> getRoleUsersByRole(Organization org, Role role) {
        StringBuilder hql = new StringBuilder("from User u where u.disabled = 'F' and u.removed = 'F' and u.orgUser <> 'T'");
        if(org != null) {
            if(org.getChildren()!= null) {
                hql.append(" and (u.organization=:org or :corg in elements(u.organization.children))");
            }else {
                hql.append(" and u.organization=:org");
            }

        }
        if(role!=null) {
            hql.append(" and :role in elements(u.roles)");
        }
        Query q = HibernateUtil.createQuery(hql.toString());
        if(org != null) {
            if(org.getChildren()!= null) {
                if(org.getParent() != null) {
                    q.setEntity("org", org).setEntity("corg", org.getParent());
                }else{
                    q.setEntity("org", org).setEntity("corg", org);
                }
            }else {
                q.setEntity("org", org);
            }
        }
        if(role!=null) {
            q.setEntity("role", role);
        }

        return list(q);
        //        return find("from User where disabled=? and removed=? and organization=? and ? in elements(roles) and ? in elements(roles)", false, false, org,role1,role2);
    }
0 0
原创粉丝点击