Hibernate 条件限制:- 如何获取当前和上个月的数据

Hibernate criteria Restrictions:- how to fetch current and last month data

这是我从创建日期获取特定月份和年份数据的方法。

    @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();
    }

那么如何写当前和上个月的限制。

我只想要 criteria.add(Restrictions.in("MONTHNAME(createdDate)", labOrderConditions.getCompareMonthList()));

如果有人有任何建议,请与我分享。

谢谢 思坦苏

从您的 labOrderConditions.getCompareMonthList() 中,在 Java 中创建相应的开始和结束日期对象,然后:

criteria.add(Restrictions.between("createdDate", startDate, endDate));

如果你有几个间隔,你也可以添加"or"限制。

这会解决您的问题,还是您希望得到另一个结果?