sql = SELECT count(*) FROM UserRoleInfo b, PersonBasicInfo a,
(SELECT distinct(u.userRoleId) FROM UserRoleAuth u where u.moduleId ='02000000000000') c 
WHERE a.personId = b.userSign 
and b.kindSign = 1 
and userRoleId = c.userRoleId 
and b.districtId like '11%' and b.userLevel = 3
getHibernateTemplate().find(sql);用hibernate 执行这条SQL都是出现不能解析的字符,com.ccidit.crsmis.usermanage.po.PersonBasicInfo这没错误啊
错误:
org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: ( near line 1, column 120 [SELECT count(*) FROM com.ccidit.crsmis.usermanage.po.UserRoleInfo b, com.ccidit.crsmis.usermanage.po.PersonBasicInfo a,(SELECT distinct(u.userRoleId) FROM com.ccidit.crsmis.usermanage.po.UserRoleAuth u where u.moduleId ='02000000000000') c WHERE a.personId = b.userSign and b.kindSign = 1 and userRoleId = c.userRoleId and b.districtId like '11%' and b.userLevel = 3]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 120 [SELECT count(*) FROM com.ccidit.crsmis.usermanage.po.UserRoleInfo b, com.ccidit.crsmis.usermanage.po.PersonBasicInfo a,(SELECT distinct(u.userRoleId) FROM com.ccidit.crsmis.usermanage.po.UserRoleAuth u where u.moduleId ='02000000000000') c WHERE a.personId = b.userSign and b.kindSign = 1 and userRoleId = c.userRoleId and b.districtId like '11%' and b.userLevel = 3]
可是我用plsql执行一点问题都没有:
SELECT count(*) FROM User_Role_Info b, Person_Basic_Info a,
(SELECT distinct(u.user_Role_Id) FROM User_Role_Auth u where u.module_Id ='02000000000000') c 
WHERE a.person_Id = b.user_Sign and b.kind_Sign = 1 and b.user_Role_Id = c.user_Role_Id and b.district_Id like '11%' and b.user_Level = 3

解决方案 »

  1.   

    用原生sql来查询
    类似如下的
    return getHibernateTemplate.execute(new HibernateCallback() {  
        public List<String> doInHibernate(Session session)  
            throws HibernateException, SQLException {  
             List results = query.list(); 
            return (List<String>)results;  
        }  
    });  参考这里http://blog.csdn.net/gtuu0123/archive/2009/12/05/4946702.aspx
      

  2.   

    and userRoleId = c.userRoleId  没问题?试试呢...
      

  3.   

    SELECT count(*) FROM UserRoleInfo b, PersonBasicInfo a,
    (SELECT distinct(u.userRoleId) FROM UserRoleAuth u where u.moduleId ='02000000000000') c  
    WHERE a.personId = b.userSign  
    and b.kindSign = 1  
    and userRoleId = c.userRoleId  
    and b.districtId like '11%' and b.userLevel = 3
    "and userRoleId = c.userRoleId " userRoleId是哪里的
    另外你可以把的where 条件优化一下 把like放到后边。总是觉得你的HQL不够好,你再看看是否可以优化的更好。 如果权限表数据很多势必影响效率。
      

  4.   

    UserRoleInfo 看到这样的,应该不是原生SQL
    HQL貌似不能做这样的查询.
      

  5.   

    楼主把几个表的关系说一下吧,HQL不支持from A a,(查询) b这样的.
      

  6.   

    你可以直接使用
    x.y.d这样的语句来连接第三张表select count(*) from  A a,B b
    where b.c.moduleId ='02000000000000'如果说你的B与C没有配置关系,那这样当然是不能使用的.
      

  7.   

    FROM UserRoleInfo b, PersonBasicInfo a,
    (SELECT distinct(u.userRoleId) FROM UserRoleAuth u where u.moduleId ='02000000000000') c