类:User
属性:userName,password,...
现在认为 如果数据库里2个记录的userName和password一样就认为是重复。现在怎么用HQL,查询出所有用户的总数,当然不能有重复记录?

解决方案 »

  1.   

    select distinct x from Column x
    在hql中,使用 * 必须要用到别名
    select distinct c.* from Column c
    查询出的结果是 null
    (我也是刚接触Hibernian,不太确定,希望能帮到您) 
      

  2.   

    hql不能在distinct,group by结果集上使用count,好像只能转成SQL来进行查询
      

  3.   

    session.createSQLQuery()
    网上很多
    http://www.javaeye.com/topic/51136
      

  4.   

    和sql类似SELECT DISTINCT userName,password FROM Users u
      

  5.   


    select count(*) from 
    (select count(*) as counts from user group by username,password) as c
      

  6.   


    依葫芦画瓢,
    我的HQL:
    String hql = "select count(*) from (select count(*) as counts from Book b group by b.title,b.anthor1,b.anthor2,b.anthor3,b.anthor4) as c";抛出如下异常??
    Caused by:
    org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, col
    umn 22 [select count(*) from (select count(*) as counts from com.bookmgr.entity.
    Book b group by b.title,b.anthor1,b.anthor2,b.anthor3,b.anthor4) as c]
      

  7.   


    select count(distinct u.userName)  from User u
      

  8.   

    select count(distinct u.userName,u.password)  from User u
      

  9.   

    HQL可以这样???
    distinct多个字段,再count???
      

  10.   

    可以用select distinct username,password from user
    然后取集合的数量或者select 1 from user group by username,password
    然后取集合的数量另外的设想:User重写equals方法比较username和password
    select count(distinct u) from User u ——没有测试过^_^另外嵌套的语句还是直接用sql吧