zoukankan      html  css  js  c++  java
  • hibernate中 criteria.setProjection(Projections.rowCount()).uniqueResult()为null的Bug

    在hibernate中,在查询总数时,会使用如下方法;


      public Integer getCount(final DetachedCriteria detachedCriteria) {
             return (Integer) getHibernateTemplate().execute(new HibernateCallback() {

                 @Override
                 public Object doInHibernate(Session session)
                         throws HibernateException, SQLException {
                     Criteria criteria = detachedCriteria.getExecutableCriteria(session);

                     int totalCount = (Integer) criteria.setProjection(
                             Projections.rowCount()).uniqueResult();

                     criteria.setProjection(null);
    //
                     criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
                     return totalCount;
                 }
             }, true);
         }

    有时在int totalCount = (Integer) criteria.setProjection(
                             Projections.rowCount()).uniqueResult();这一句会报NullPointException。

    原因是这句代码之前出现了criteria.addOrder(Order.desc("sc.createTime"));这样的排序语句。

    把排序语句放到总数查询的后面即可。有时候你注释了缓存也仍然会影响的,需要把注释的语句也删掉。

  • 相关阅读:
    悲剧的程序员
    【C++】关于随机函数与概率设置
    布局管理器(一)
    敏捷开发,如何搜集故事
    【Visual C++】CDC与HDC的区别以及相互转换
    IFRAME without src attribute on HTTPS in Internet Explorer
    测量某断代码执行时间-代码
    数据一致性实现技术
    ffmpeg
    Log4j的使用方法
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/4329484.html
Copyright © 2011-2022 走看看