0

我有一个实体

public class CommissionSummary {    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(length = 100)
    private String advisorCode;
    @Column(length = 100)
    private String advisorName;
    private String advisorCodeParent;
    @Column(length = 100)
    private String advisorNameParent;
    @Column(length = 100)
    private String advisorPost;
    @Column
    private Double percentage;
    @Column
    private Double diffPercentage;
    @Column
    private Double saleAmount;
    @Column
    private Long saleCount;
    @Column
    **private Double commissionAmount;
    @Column
    private Integer month;
    @Column
    private Integer year;**
    //Getter Setter
}

在屏幕上,用户正在输入标准以获取两个日期之间的日期。

前任。从 2012 年 1 月 1 日到 2012 年 7 月 30 日。

在 CommissionSummary 实体中没有日期列,但它有月份和年份 2 单独的列。

我想根据月份和年份列获取用户给出的从日期到日期期间的 CommissionSummary 记录。

那么如何使用 Hibernate Criteria / Restrictions 来实现这一点呢?

注意:日期字段在用户输入的日期和日期中没有任何意义。

4

4 回答 4

3

您可以将标准分解为三个较小标准的析取:

  1. 佣金的年份等于查询的起始年份,其月份大于等于查询的起始月份
  2. 佣金的年份大于查询的开始年份且小于其结束年份
  3. 佣金的年份等于查询的最后年份,其月份小于或等于查询的结束月份

您可以将其中的每一个写成两个简单比较的结合。这看起来像(未经测试!):

int fromYear, fromMonth, toYear, toMonth;
Property year = Property.forName("year");
Property month = Property.forName("month");
session.createCriteria(CommissionSummary.class).add(Restrictions.disjunction()
    .add(Restrictions.and(year.eq(fromYear), month.ge(fromMonth))
    .add(Restrictions.and(year.gt(fromYear), year.lt(toYear))
    .add(Restrictions.and(year.eq(toYear), month.le(toMonth))
);
于 2012-07-16T19:14:08.033 回答
0

这样做没有任何限制。您必须创建自己的 Criterion 子类来生成适当的 SQL,或者使用Restrictions.sqlRestriction().

在 SQL 中,在 Oracle 上,SQL 限制可能如下所示:

to_date(c.year || '-' || c.month || '-01', 'YYYY-MM-DD') between :startDate and :endDate)
于 2012-07-16T09:21:38.457 回答
-1

您可以使用 HQL 来选择您想要的内容:

session.beginTransaction();
session.clear();

Query query = session.createSQLQuery(" from CommissionSummary CS  where  to_date(CS.year || '-' || CS.month || '-01', 'YYYY-MM-DD') between :startDate and :endDate)"

List result = query.list();
于 2012-07-16T09:20:58.710 回答
-1

感谢您的所有回答@JB Nizet 告诉了正确的方法,但它来自本机 SQL。我在下面尝试过成功....

public List<CommissionSummary> getCommissionSummary(AdvisorReportForm advisorReportForm) {
        Criteria criteria = getSession().createCriteria(CommissionSummary.class);
        if (advisorReportForm.getAdvisorId() != null && advisorReportForm.getAdvisorId() > 0) {
            criteria.add(Restrictions.eq("advisorCode", advisorReportForm.getAdvisorId().toString()));
        }

        if (advisorReportForm.getFromDate() != null && advisorReportForm.getToDate() != null) {
            Calendar calFrom = Calendar.getInstance();
            calFrom.setTime(advisorReportForm.getFromDate());

            Calendar calTo = Calendar.getInstance();
            calTo.setTime(advisorReportForm.getToDate());

            Criterion crit1 = Restrictions.eq("month", calFrom.get(Calendar.MONTH) + 1);
            Criterion crit2 = Restrictions.eq("year", calFrom.get(Calendar.YEAR));
            Criterion critMonthYear1 = Restrictions.and(crit1, crit2);
            calFrom.add(Calendar.MONTH, 1); // increment loop by month

            Criterion critAll = critMonthYear1;
            while (calFrom.getTimeInMillis() < calTo.getTimeInMillis()) {
                Criterion crit1Loop = Restrictions.eq("month", calFrom.get(Calendar.MONTH) + 1);
                Criterion crit2Loop = Restrictions.eq("year", calFrom.get(Calendar.YEAR));
                Criterion critMonthYearLoop = Restrictions.and(crit1Loop, crit2Loop);
                critAll = Restrictions.or(critAll, critMonthYearLoop);
                calFrom.add(Calendar.MONTH, 1); // increment loop by month
            }
            criteria.add(critAll);

        }

        return criteria.list();
    }

从日期到日期的差异最多可验证 6 个月。我没有看到太多的性能问题。所以就用了这个。

此处生成的 SQL 供您参考

SELECT * FROM CommissionSummary this_ 
WHERE this_.advisorCode=1 
AND (((((this_.month=11 AND this_.year=2011) OR (this_.month=12 AND this_.year=2011)) OR (this_.month=1 AND this_.year=2012)) 
OR (this_.month=2 AND this_.year=2012)) OR (this_.month=3 AND this_.year=2012))
于 2012-07-16T10:19:51.140 回答