public List
Criteria criteria = getSession().createCriteria(MyEntity.class);
criteria.add(getDateCriteria(afterDate, beforeDate));
return criteria.list();
}
public List
Criteria criteria = getSession().createCriteria(MyEntity.class);
criteria.add(getDateCriteria(afterDate, beforeDate));
criteria.add(Restrictions.eq("type", type));
return criteria.list();
}
private Criterion getDateCriteria(Date afterDate, Date beforeDate) {
if(beforeDate == null) { // Is parameter provided ?
beforeDate = new Date();
}
if(afterDate == null) { // Is parameter provided
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -1);
afterDate = cal.getTime();
}
Criterion start = Restrictions.between("startDate", beforeDate, afterDate);
Criterion end = Restrictions.between("endDate", beforeDate, afterDate);
return Restrictions.or(start, end);
}
No comments:
Post a Comment