select * from SCOTT.TEACHER teacher3_ where teacher3_.POST='管理员        ' or teacher3_.DEPARTID='004' and teacher3_.POST='系教学秘书    ' or teacher3_.POST='系主任        ' or( teacher3_.POST='教研室主任    ' and teacher3_.SPECID='0002')
是否等于
select * from SCOTT.TEACHER teacher3_ where teacher3_.POST='管理员        ' or teacher3_.DEPARTID='004' and teacher3_.POST='系教学秘书    ' or teacher3_.POST='系主任        ' or teacher3_.POST='教研室主任    ' and teacher3_.SPECID='0002'自己测试了一下,感觉括号好像没有有优先作用,好像被oracle数据库给忽略了。
我要查询的是管理员教师,同一个系部下的教学秘书教师,系主任教师,或者同一个系部下教师研主任教师且在同一个专业下。and or的执行顺序好像是同一级别的哦,按照下面的hql语写,哪里有错啊?
在hibernate中执行
"from Teacher teacher where teacher.post = '"+ UserPost.admin+"' or (teacher.department.departid ='"+student.getDepartment().getDepartid()+"' and " +"teacher.post = '" + UserPost.secretary +"' or teacher.post ='"+UserPost.director +"' or (teacher.post ='"+UserPost.teamDirector +"' and teacher.speciality.specid = '"+ student.getSpeciality().getSpecid() + "')))";
查看对应的sql语句切是
select * from SCOTT.TEACHER teacher3_ where teacher3_.POST='管理员        ' or teacher3_.DEPARTID='004' and teacher3_.POST='系教学秘书    ' or teacher3_.POST='系主任        ' or teacher3_.POST='教研室主任    ' and teacher3_.SPECID='0002'
hibernate好像忽略那两个括号.
不知道是为什么。

解决方案 »

  1.   

    因为and的优先级比or高。所以你这里没有体现啦。
    要不你换个
    select * from tbl where (a='b' or a='c') and b='c'
    然后再把括号去掉看看?你会发现a='b'的但是b<>'c'的也出来了。
      

  2.   

    1
    括号()有优先级高的作用2
    and比or的优先级高 这也是为什么你加括号和取掉括号两者执行结果一样的原因
      

  3.   

    WHERE 
    (lb='类别' and cid is null ) or ( lb='类别' and mc='名称'and cid is not null)
    经过hibernate处理后括号发生变化