商家税表:Id,税额,时间,商家id(外键);
商家表:id,名称;说明:商家在一年中,缴纳次数不定,在商家税表中存放的商家id有重复值。如果要查询2000年,交税的商家数量
采用sql语句:select count(distinct 商家id) from table 商家税表现在要采用hibernate进行查询,如何写呢?

解决方案 »

  1.   

    Configuration cfg=new Configuration().configure();
    SessionFactory sf=cfg.buildSessionFactory();
    Session s=sf.openSession();
    Criteria c=s.createCriteria(商家税表.class);
    往下就不知道怎么写了???
      

  2.   

    // 查询总记录数 
            Criteria criteria = getSession().createCriteria(Files.class); 
            criteria.add(Example.create(instance)); 
            criteria.setProjection(Projections.rowCount());   
                List results = criteria.list(); 
                int totalcount = Integer.parseInt(results.get(0).toString()); 
            page.setTotalCount(totalcount); 
      

  3.   

    int rows= ((Integer)session.createQuery(sql).list().iterator().next()).intValue();  
      

  4.   

    ((Long)sessionFactory.getCurrentSession().createQuery("select count(*) from ReceivedEmail").iterate().next()).intValue(); 
      

  5.   

    查到这样一句:
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        //设置ENTITY级的DISTINCT模式,根实体但是不知道如何来限制"商家id"的唯一性。
      

  6.   

    select count(商家ID) from 商家税表 group by 商家ID
      

  7.   

    不是写sql语句,而是让hibernate自己生成sql语句,
      

  8.   

    不是写sql语句,而是让hibernate自己生成sql语句,.........
      

  9.   

    你一样的写,hql可以这么写:select count(distinct 商家id) from class 商家税类名
      

  10.   

    http://heipark.javaeye.com/blog/378540