程序员一代码搬运工
【Java学习心得和Java学习技术个人总结】

hibernate Criterion criteria 多表查询

List<Criterion> criterions = new ArrayList<Criterion>();
Criteria criteria = createCriteria(Report.class);

if (user != null) {
criteria.add(Restrictions.eq("user", user));
}

if (StringUtil.isNotEmpty(reportInfo.getReportSchedule()) ) {

criteria.add(Restrictions.eq("reportSchedule", ReportSchedule.convert_value(reportInfo.getReportSchedule())));
}
if (StringUtil.isNotEmpty(reportInfo.getName())) {
criteria.add(Restrictions.like("name","%"+reportInfo.getName()+"%")); 
}
if(reportInfo.getDate()!=null){
Date date1 = null;
try {
date1 = DateUtil.getDateByString(reportInfo.getDate()+" 00:00:00");

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date date2 = null;
try {
date2 = DateUtil.getDateByString(reportInfo.getDate()+" 23:59:59");
System.out.println(date2);
} catch (ParseException e) {

e.printStackTrace();
}
criteria.add(Restrictions.between("createOn",date1,date2)); 
}
if(StringUtil.isNotEmpty(reportInfo.getActivityType())){

Criteria criteria1 = criteria.createCriteria("reportDetail");
criteria1.add(Restrictions.eq("activityType", ActivityType.convert_value(reportInfo.getActivityType())));
}
if(StringUtil.isNotEmpty(type)){
if(type.equals("all")){
//所有
}else if(type.equals("finish")){
//已完成
criteria.add(Restrictions.eq("reportSchedule", ReportSchedule.SUCCESS));

}else if(type.equals("unfinish")){
criteria.add(Restrictions.ne("reportSchedule", ReportSchedule.SUCCESS)); 
}
}
criteria.addOrder(Order.asc("createOn"));
if( page != null) {
int totalCount = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
page.setTotalCount(totalCount);

criteria.setProjection(null);
criteria.setFirstResult((page.getPageNum() - 1) * page.getNumPerPage()).setMaxResults(page.getNumPerPage());
} 

return criteria.list();

 

赞(0)
未经允许不得转载:程序员一代码搬运工 » hibernate Criterion criteria 多表查询
分享到: 更多 (0)