使用限制的Hibernate Criteria

时间:2012-07-16 09:03:51

标签: java hibernate hibernate-criteria

我有一个实体

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
}

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

实施例。从2012年1月1日至2012年7月30日。

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

我想根据月份和年份列从用户提供的日期和日期获取CommissionSummary记录。

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

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

4 个答案:

答案 0 :(得分:3)

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

  1. 佣金的年份等于查询的开始年份,其月份大于或等于查询的开始月份
  2. 佣金的年份大于查询的开始年份且小于其结束年度
  3. 佣金的年份等于查询的年度,其月份小于或等于查询的结束月份
  4. 您可以将每个作为两个简单比较的结合来编写。这看起来像(未经过测试!):

    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))
    );
    

答案 1 :(得分:0)

没有限制可以做到这一点。您必须创建自己的Criterion子类,以生成相应的SQL,或使用Restrictions.sqlRestriction()

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

to_date(c.year || '-' || c.month || '-01', 'YYYY-MM-DD') between :startDate and :endDate)

答案 2 :(得分:-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();

答案 3 :(得分:-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))