hibernate criteria projections 映射使用

Criteria criterion=super.getSession().createCriteria(PaymentRecord.class);
        criterion.add(
            Restrictions.and(
                Restrictions.eq("worker", worker),
                Restrictions.eq("status", Constants.paymentrecord_status_confirmed),
                Restrictions.ge("createTime", start),
                Restrictions.le("createTime", end)
            ));//传递restrictions ,增加查询条件
        criterion.setProjection(
            Projections.projectionList()
                .add(Projections.sqlGroupProjection(
                    "sum(amount) as amount,date(createTime) as day", //设置计算函数和返回值
                    "DATE(createTime) order by day asc", //设置group ,并且加入order
                    new String[]{"amount","day"},//记录 值变量
                    new Type[]{BigDecimalType.INSTANCE,DateType.INSTANCE}))//设置值类型
              //  .add(Projections.groupProperty("worker"))//在此可增加group property
                .add(Projections.rowCount())
            );
        criterion.setResultTransformer(Transformers.aliasToBean(DaysIncome.class));//设置 bean vo,此bean的属性要和以上 查询返回的属性一致包括类型,否则 会最终出现异常
        @SuppressWarnings("unchecked")
        List<DaysIncome> ls=criterion.list();
        return ls;
华丽的分割线,以下为一复杂应用 -----------------------------------------------------
 Criteria categoryCriteria = sessionFactory.getCurrentSession().createCriteria(TaskCategorySummary.class, "c");
//                .createAlias("c.favorite","f");
        categoryCriteria.createAlias("requesterAccount", "r");
//                .setProjection(Projections.count("f.id").as("favoriteCount"))
        categoryCriteria.setProjection(Projections.projectionList()
                        .add(Projections.property("r.name").as("requesterAccount"))
                        .add(Projections.property("r.companyName").as("companyName"))
                        .add(Projections.property("r.logoUrl").as("logoUrl"))
                        .add(Projections.property("c.id").as("categoryId"))
                        .add(Projections.property("c.name").as("name"))
                        .add(Projections.property("c.categoryUuid").as("categoryUuid"))
                        .add(Projections.property("c.unitPrice").as("unitPrice"))
                        .add(Projections.property("c.workerGrade").as("workerGrade"))
                        .add(Projections.property("c.codeOrderNumber").as("codeOrderNumber"))
                        .add(Projections.property("c.deductable").as("deductable"))
                        .add(Projections.property("c.approvalTimeInHours").as("approvalTimeInHours"))
                        .add(Projections.property("c.taskCount").as("taskCount"))
                        .add(Projections.property("c.remainedCount").as("remainedCount"))
                        .add(Projections.property("c.startTime").as("startTime"))
                        .add(Projections.property("c.expiredTime").as("expiredTime"))
                        .add(Projections.property("c.onlineWorkers").as("onlineWorkers"))
                        .add(Projections.property("c.ordinalNum").as("taskOrdinalNum"))
                        .add(Projections.property("c.lastPackageUploadTime").as("lastPackageUploadTime"))
                        .add(Projections.property("c.parentId").as("parentId"))
//                        .add(Projections.sqlProjection("count(f.id) as favoriteCount", new String[]{"favoriteCount"}, new Type[]{LongType.INSTANCE}))
//                        .add(Projections.count("fav.id").as("favoriteCount"))
//                        .add(Projections.property("favoriteCount").as("favoriteCount"))

                )
                .add(Expression.eqProperty("c.requesterAccount", "r.id"))
//                .add(Expression.eqProperty("c.categoryUuid", "f.categoryUuid"))
                .add(Restrictions.eq("c.categoryUuid", parentCategoryUuid))
                .addOrder(Order.asc("c.id"))
                .setFirstResult((page - 1) * pageSize)
                .setMaxResults(pageSize)
                .setResultTransformer(Transformers.aliasToBean(TaskCategoryDto.class));
//                .setResultTransformer(new AliasToBeanResultTransformer(TaskCategoryDto.class));

        Long count = DaoHelper.getRowCount(categoryCriteria);
        List<TaskCategoryDto> result = categoryCriteria.list();
        return new PaginationRecordsAndNumber<>(result, count);
在此需要说明的是 使用criteria ,如果 是多表查询 ,那么需要存在hibernate的映射关系,否则将查询使用,出现主从属性不存在的异常,那么此时只有使用hql了 :)


你可能感兴趣的:(Hibernate,Projections)