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"));这样的排序语句。

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

  • 相关阅读:
    腾讯实习前端工程师面经-一面-腾讯看点
    Redux的createStore实现
    GNU ARM 汇编基础
    python爬虫学习04-爬取贴吧
    python学习03-使用动态ua
    Python爬虫学习02--pyinstaller
    python爬虫学习01--电子书爬取
    简单的SQL语句学习
    微信小程序的五个生命周期函数
    python学习(12)使用正则表达式
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/4329484.html
Copyright © 2011-2022 走看看