Hibernate和Mysql结果不匹配

时间:2011-10-21 17:19:36

标签: java mysql hibernate

我正在使用Hibernate连接到mysql作为数据库层,奇怪的是出于某种原因 从Hibernate api java代码执行的结果集与直接从mysql执行的结果集不同。他们不是彼此的一部分,他们看起来没什么关系。

这是Dao层的Java代码:

    int totalPage = 0;
    int reminder = total % pagination.getPageSize();
    if(total == 0){
        totalPage = 1;
        pagination.setTotalPage(totalPage);
    }else{
        totalPage = reminder == 0 ? total / pagination.getPageSize() : (total - reminder) / pagination.getPageSize() + 1;
        pagination.setTotalPage(totalPage);
    }

    pagination.setTotalRows(total);
    pagination.setBeginAndEndPage(total);

    final StringBuilder queryString = new StringBuilder();
    queryString.append(" select a.target_id as itemId, a.isFree as isFree, ");
    queryString.append(" ni.fullName as item, nc.fullName as category, a.downloadCount as downloadCounts from( ");
    queryString.append(" select dldc.current_category_id, dldc.target_id, dldc.isFree, ");
    queryString.append(" sum(dldc.counts) as downloadCount from download_log_day_count dldc ");
    queryString.append(" group by dldc.current_category_id, dldc.target_id) as a ");
    queryString.append(" left join item i on a.target_id = i.objectId ");
    queryString.append(" left join name ni on i.nameId = ni.objectId ");
    queryString.append(" left join category c on a.current_category_id = c.objectId ");
    queryString.append(" left join name nc on c.nameId = nc.objectId ");
    queryString.append(" order by downloadCounts desc");

    List<ItemReportVO> reportList = (List<ItemReportVO>)getJpaTemplate().execute(new JpaCallback(){

        @Override
        public Object doInJpa(EntityManager em)
                throws PersistenceException {
            SQLQuery query = ((Session)em.getDelegate()).createSQLQuery(queryString.toString());
            query.addScalar("itemId", Hibernate.LONG);
            query.addScalar("isFree", Hibernate.BOOLEAN);
            query.addScalar("item", Hibernate.STRING);
            query.addScalar("category", Hibernate.STRING);
            query.addScalar("downloadCounts", Hibernate.LONG);
            query.setResultTransformer(Transformers.aliasToBean(ItemReportVO.class));
            int firstResult = (pagination.getCurrentPage()-1) * pagination.getPageSize();
            query.setFirstResult(firstResult);
            if(token != null && "all".equals(token)){
                int maxResult = pagination.getPageSize();
                query.setMaxResults( maxResult );
            }else{
                int maxResult = pagination.getPageSize() > Integer.valueOf(token) ? Integer.valueOf(token) : pagination.getPageSize();
                query.setMaxResults( maxResult );
            }
            return query.list();
        }
    });

当我在Hibernate的配置文件中设置show_sql = true时,Hibernate在Java代码运行时打印下面的sql查询“return query.list();”:

    select a.target_id as itemId, a.isFree as isFree,  ni.fullName as item, nc.fullName as category, a.downloadCount as downloadCounts 
from(  
    select dldc.current_category_id, dldc.target_id, dldc.isFree,  sum(dldc.counts) as downloadCount 
    from download_log_day_count dldc  
    group by dldc.current_category_id, dldc.target_id) as a  
left join item i on a.target_id = i.objectId  
left join name ni on i.nameId = ni.objectId  
left join category c on a.current_category_id = c.objectId  
left join name nc on c.nameId = nc.objectId 
order by downloadCounts desc 
limit ?

我一直认为它们应该是一样的,但似乎它们不是,任何人都可以帮我调整java代码?

1 个答案:

答案 0 :(得分:1)

Hibernate会更改您的查询以应用您通过API setMaxResults()编码的限制。它还应为rownum>添加setFirstResult(),但在您的情况下,它可能是第一次查询。

您不必更改任何内容,因为Hibernate以特定于数据库的方式应用这些内容。

您有任何其他不同之处吗?