Hibernate HQL和原生SQL查询的一点区别

1.createSQLQuery

  1.1默认查询的结果为BigDecimal

  1.2通过addScalar("CGD_ID", StandardBasicTypes.LONG)可以将结果直接转为Long

     StringBuffer sb = new StringBuffer();
        sb.append("select g.CGD_ID from em_circle_apply_info a join em_circle_group_def g on a.CAI_CGD_ID = g.CGD_ID ");
        if (size != null) {
            sb.append(" where rownum < " + (size+1));
        }
        sb.append(" group by a.CAI_CGD_ID order by count(a.CAI_CGD_ID) desc ");
        Query query = this.getSession().createSQLQuery(sb.toString()).addScalar("CGD_ID", StandardBasicTypes.LONG);

2.createQuery

  2.1默认查询结果为Long

     StringBuffer sb = new StringBuffer();
     sb.append("select t.caiCgdId.cgdId from EmCircleApplyInfo t "); if (size != null) { sb.append(" where rownum < " + (size+1)); } sb.append(" group by t.caiCgdId.cgdId order by count(t.caiCgdId) desc"); Query query = this.getSession().createQuery(sb.toString()).setCacheable(true); List<BigDecimal> idList = query.list(); List<Long> ids = new ArrayList<Long>(); List<Long> ids = query.list();
原文地址:https://www.cnblogs.com/luoxiaolei/p/5665638.html