如果是使用hibernate3,则直接写,hibernate3支持子查询,也可以考虑使用关联

解决方案 »

  1.   

    直接用sql语句好了,何必一定要用hsql
      

  2.   

    select
        auth.authorName,
        count(case when book.category = 1 then 1 else null end) as type1count,
        count(case when book.category = 2 then 1 else null end) as type2count
    from
        package.Book book
        inner join book.author auth
    group by
        auth.authorName
      

  3.   

    我试了一下用count好像不行,换种写法如下
    select
        new Map(
            auth.authorName,
            sum(case when book.category = 1 then 1 else 0 end) as type1count,
            sum(case when book.category = 2 then 1 else 0 end) as type2count
        )
    from
        package.Book book
        inner join book.author auth
    group by
        auth.authorName
      

  4.   

    TO diaopeng(放飞自己):
    是么,虽然支持子查询,那我给的那样的SQL语句怎样直接写实现呢?
      

  5.   

    统计还用hql干吗?这根本不是hibernate该干的事儿
      

  6.   

    为什么?直接改SQL,那换一种数据库呢?把所有统计代码全部都重新检查一遍?自己实现了此分类统计,不过是用两个HQL结果集合并的,看了一下生成的SQL,效率还行。多谢dreamover的提醒
      

  7.   

    如果要考虑到换db,可以写在proc里面去啊,到时候改proc就好了,这个不是hibernate的强项,是Ibatis的强项