hibernate criteria projections 映射使用
2013-06-02 12:14
411 查看
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 中的 DetachedCriteria查询(所有使用到的类属性必须在映射文件中存在)
- Hibernate中使用Criteria接口的Projections类处理聚合结果
- Hibernate中Criteria的使用
- 【转载】Hibernate之hbm.xml集合映射的使用(Set集合映射,list集合映射,Map集合映射)
- (转)使用myeclipse生成实体类和hibernate映射文件
- Hibernate使用Criteria查询部分字段
- Hibernate 配置使用线程报错org.hibernate.HibernateException: createCriteria is not valid without active trans
- Hibernate 查询 Projections 类使用
- Hibernate3 关系映射使用说明(一)
- Hibernate使用Map实现多对多映射
- Intellj IDEA 使用Hibernate5.2.3 新建映射文件步骤
- Hibernate使用List实现一对多映射
- Hibernate中使用Criteria查询及注解——(Emp.hbm.xml)
- Hibernate中使用Criteria查询及注解——( EmpCondition)
- hibernate基本的配置-xml(实体映射)和简单使用
- Hibernate里面如何使用DetachedCriteriaCriteria 实现多条件分页查询
- Hibernate 多表关联映射- Hibernate中使用的集合类型(set,list,array,bag,map)
- 【Hibernate】——使用连接表的单向关联映射
- hibernate中 criteria.setProjection(Projections.rowCount()).uniqueResult()为null的Bug
- 使用Spring 来配置 hibernate 映射文件的 两种方法