数据表:
comment{ id, user_id, parent_id } // parent_id指被评论的comment的idhibernate对象映射(简化版):
Comment{ Long id, Long user_id, Comment parent }数据库数据(示意版):
comment表:
id user_id parent_id
1     1       null
2     2       1
也就是说,user 1发表了一篇评论,user 2评论了user 1的评论目标:查找user 1发出和收到的评论(本来理论上应该为2,但是):HQL语句:
1.查找收到的评论:
select count(*) from Comment where parent.user_id=1;
结果是12.查到发出的评论:
select count(*) from Comment where user_id=1;
结果是13.查找全部的评论:
select count(*) from Comment where parent.user_id=1 or user_id=1;
结果却仍然是1后来我查看了HQL生成的SQL语句,其中的where部分括号位置不合要求,简写如下:
... where comment0_.parent_id=comment1_.id and (comment1_.user_id=1 or comment0_.user_id=1)
他居然先入为主地先将两个表做连接了,而我的本意应该是改变一下括号的顺序:
... where (comment0_.parent_id=comment1_.id and comment1_.user_id=1) or comment0_.user_id=1
甚至都不用加括号,我觉得我的HQL语句没错,但是不知道怎么会生成这样的SQL语句,我自己又没法控制!不知道能有谁指点一下正确的HQL语句吗?(虽然我的HQL语句在逻辑上算也是正确的)

解决方案 »

  1.   

    看的云里雾里的,HQL 很强大,应该是你设计思路的问题。实在不行就用原生的 sql.
      

  2.   

    id user_id parent_id 
    1    1      null 
    2    2      1 user_id为1的本来就只有一条啊!
      

  3.   

    user_id为1的记录是user 1发出的评论,但是还需要查找 parent.user_id=1 的记录(即user 1收到的评论)
      

  4.   

    comment表: 
    id user_id parent_id 
    1    1      null 
    2    2      1 
    也就是说,user 1发表了一篇评论,user 2评论了user 1的评论 目标:查找user 1发出和收到的评论(本来理论上应该为2,但是): HQL语句: 
    1.查找收到的评论: 
    select count(*) from Comment where parent.user_id=1; 
    结果是1 2.查到发出的评论: 
    select count(*) from Comment where user_id=1; 
    结果是1 3.查找全部的评论: 
    select count(*) from Comment where parent.user_id=1 or user_id=1; 
    结果却仍然是1 
    和你的表对了一下,好像都对,没有错吧
      

  5.   

    对啊,HQL的确很强大,但是这么强大的HQL居然连我想要的基本意思都生成错误,难免不让人感到有些失望
      

  6.   

    我已经把HQL生成的SQL语句贴出来了,在where子句部分错误,见楼主帖子的末尾
      

  7.   

    你的思路是不是有点问题?大概的理解了一下你是不是要查user_id=1 和 parent_id=1的数据?
    那HQL语句就直接可以写为
    select count(*) from Comment user_id=1 or parent_id=1在hibernate映射文件里面不需要配置映射关系的,要不然的话肯定会有问题
      

  8.   

    你理解的不完全正确,对于我举的那个comment表的例子:
    id user_id parent_id 
    1    1      null      注:user 1发出的评论
    2    2      1         注:user 2给user 1的评论,即user 1收到的评论
    用你的HQL能够查到结果我想要的结果,但是如果数据变化后,如下:
    id user_id parent_id 
    11   1      null      注:user 1发出的评论
    21   2      11        注:user 2给user 1的评论,即user 1收到的评论
    用你的查询语句就得不到查询user 1收到的和发出的全部评论了
    所以必须确定parent.user_id=1才行
      

  9.   

    改成:select count(*) from Comment where (parent.user_id=1) or (user_id=1 and parent is null) 
      

  10.   

    您好,你的HQL语句生成的SQL语句的where子句为:
    where
            comment0_.parent_id=comment1_.id 
            and (
                comment1_.user_id=? 
                or comment0_.user_id=? 
                and (
                    comment0_.parent_id is null
                )
            )明显第一个and连接就错了吗,我的本意应该是:
    where
            comment0_.parent_id=comment1_.id 
            and (
                comment1_.user_id=? 
            )
            or comment0_.user_id=?
      

  11.   


    这个地方HQL怎么会有错,
    你的select count(*) from Comment where parent.user_id=1 or user_id=1这一句给他的意思本来就是这样,你的parent.user_id并等同于parent_id,你是不是在数据库中自己与自己表建立了主外键关系??
    如果是的话,就得改一下Pojo和配置文件,必须有一个字段直接关系parent_id而不是以对象形式关联
      

  12.   

    终于用外连接解决了!!!HQL语句: from Comment c left join c.parent p where c.user_id=? or p.user_id=?