zoukankan      html  css  js  c++  java
  • 转载:criteria.setProjection(Projections.rowCount()

    今天把myblog用sql server部署了一下,可是发现分页查询的时候出现错误,看控制台报错说语句有错,由来发现分页的时候先查询总记录数目的那条语句出错了

    select count(*as y0_ from myblog_Blog this_ inner join myblog_Blog_Category categories3_ on this_.id=categories3_.blogId inner join myblog_Category category1_ on categories3_.categoryId=category1_.id where category1_.id=order by this_.postTime desc

          原来开发的时候我是用的mysql,没有任何问题。原因就在最后面的order by 语句,sql server 在select count(*)里面不能用 order by。然后跟踪代码发现:

        public PaginationSupport getBlogsByCategoryByPage(final String categoryId, final int startIndex, final int pageSize) {
            
    return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
                
    public Object doInHibernate(Session session) throws HibernateException {
                    Criteria criteria 
    = session.createCriteria(Blog.class);
                    Criteria cateCriteria 
    = criteria.createCriteria("categories");
                    cateCriteria.add(Expression.eq(
    "id",categoryId));
                    criteria.addOrder(Order.desc(
    "postTime"));
                    
    int totalCount = ((Integer) criteria.setProjection(Projections.rowCount())
                            .uniqueResult()).intValue();
                    criteria.setProjection(
    null);
                    
                    List items 
    = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
                    List blogs 
    = new ArrayList();
                    
    for(Iterator ite = items.iterator(); ite.hasNext();) {
                        Object[] objs 
    = (Object[])ite.next();
                        blogs.add(objs[
    1]);
                    }
                    PaginationSupport ps 
    = new PaginationSupport(blogs, totalCount, pageSize, startIndex);
                    
    return ps;
                }
            }, 
    true);
        }

    原来问题就在Criteria.addOrder(Order.desc("postTime"));这句话的位置上面,int totalCount = ((Integer) criteria.setProjection(Projections.rowCount())
          .uniqueResult()).intValue();
    这句话的时候就会生成上面那句话,如果在这之前addOrder就会出现问题,如果你用mysql不会出现问题,如果你用sql server就会报错。解决方法就是把addOrder语句放到totalCount下面就可以了。

        public PaginationSupport getBlogsByCategoryByPage(final String categoryId, final int startIndex, final int pageSize) {
            
    return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
                
    public Object doInHibernate(Session session) throws HibernateException {
                    Criteria criteria 
    = session.createCriteria(Blog.class);
                    Criteria cateCriteria 
    = criteria.createCriteria("categories");
                    cateCriteria.add(Expression.eq(
    "id",categoryId));
                    
    int totalCount = ((Integer) criteria.setProjection(Projections.rowCount())
                            .uniqueResult()).intValue();
                    criteria.setProjection(
    null);
                    
                    
    /*
                     * Fix a bug ,Order must add after get the totalCount,
                     * beacuse SqlServer not support order by in the select count(*).
                     
    */
                    criteria.addOrder(Order.desc(
    "postTime"));
                    List items 
    = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
                    List blogs 
    = new ArrayList();
                    
    for(Iterator ite = items.iterator(); ite.hasNext();) {
                        Object[] objs 
    = (Object[])ite.next();
                        blogs.add(objs[
    1]);
                    }
                    PaginationSupport ps 
    = new PaginationSupport(blogs, totalCount, pageSize, startIndex);
                    
    return ps;
                }
            }, 
    true);
        }


    这样生成的sql语句就是这样的。

    select count(*as y0_ from myblog_Blog this_ inner join myblog_Blog_Category categories3_ on this_.id=categories3_.blogId inner join myblog_Category category1_ on categories3_.categoryId=category1_.id where category1_.id=?
  • 相关阅读:
    PAT (Advanced Level) 1060. Are They Equal (25)
    PAT (Advanced Level) 1059. Prime Factors (25)
    PAT (Advanced Level) 1058. A+B in Hogwarts (20)
    PAT (Advanced Level) 1057. Stack (30)
    PAT (Advanced Level) 1056. Mice and Rice (25)
    PAT (Advanced Level) 1055. The World's Richest (25)
    PAT (Advanced Level) 1054. The Dominant Color (20)
    PAT (Advanced Level) 1053. Path of Equal Weight (30)
    PAT (Advanced Level) 1052. Linked List Sorting (25)
    PAT (Advanced Level) 1051. Pop Sequence (25)
  • 原文地址:https://www.cnblogs.com/liuhouhou/p/2383397.html
Copyright © 2011-2022 走看看