Here is my method to get the specific month and year data from created date.
@Override
public List getMonthlyOrdersSummary(LabOrderConditions labOrderConditions) {
Criteria criteria = getCurrentSession().createCriteria(LabOrder.class);
criteria.add(Restrictions.gt("orderNo", 0));
if (StringUtils.isNotBlank(labOrderConditions.getClientCode()))
criteria.add(Restrictions.eq("clientCode", labOrderConditions.getClientCode()));
if (labOrderConditions.getAccountCodesList() != null) {
criteria.add(Restrictions.in("practice.code", labOrderConditions.getAccountCodesList()));
}
if (StringUtils.isNotBlank(labOrderConditions.getClientsCodes())) {
if (labOrderConditions.getCallingPlace().equalsIgnoreCase("allClients")) {
List<String> items = Arrays.asList(labOrderConditions.getClientsCodes().split("\\s*,\\s*"));
criteria.add(Restrictions.in("clientCode", items));
}
}
criteria.add(Restrictions.sqlRestriction("YEAR(createdDate)=" + labOrderConditions.getYear()));
ProjectionList projectionList = Projections.projectionList();
if (labOrderConditions.getCallingPlace().equalsIgnoreCase("allClients")) {
projectionList.add(Projections.groupProperty("clientCode"), "clientCode");
}
projectionList.add(Projections.sqlGroupProjection("MONTHNAME(createdDate) as orderMonthName", "MONTHNAME(createdDate)",
new String[] { "orderMonthName" }, new Type[] { StandardBasicTypes.STRING }));
projectionList.add(Projections.sqlGroupProjection("Month(createdDate) as orderMonthNo", "Month(createdDate)", new String[] { "orderMonthNo" },
new Type[] { StandardBasicTypes.INTEGER }));
projectionList.add(Projections.rowCount());
criteria.setProjection(projectionList);
criteria.addOrder(org.hibernate.criterion.Order.asc("createdDate"));
return criteria.list();
}
So how to write restriction for current and last month.
I just want something like criteria.add(Restrictions.in("MONTHNAME(createdDate)", labOrderConditions.getCompareMonthList()));
Please if anyone have any suggestion share with me.
Thanks Sitansu
From your labOrderConditions.getCompareMonthList(), create the corresponding start and end Date objects in Java, then:
criteria.add(Restrictions.between("createdDate", startDate, endDate));
If you have several intervals, you may also add "or" restrictions.
Would that solve your problem, or do you expect another result?