Java 使用限制休眠条件
我有一个实体作为Java 使用限制休眠条件,java,hibernate,hibernate-criteria,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; pri
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中,实体没有date列,但它有month和year 2两个单独的列
我想根据月和年列获取用户给定的从日期到日期期间的佣金汇总记录
那么,如何使用Hibernate标准/限制来实现这一点呢
注意:日期字段在用户输入的起始日期和截止日期中没有任何意义。您可以使用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();
这样做没有限制。您必须创建自己的Criteria子类来生成适当的SQL,或者使用
Restrictions.sqlRestriction()
在SQL中,在Oracle上,SQL限制可能如下所示:
to_date(c.year || '-' || c.month || '-01', 'YYYY-MM-DD') between :startDate and :endDate)
感谢您的所有回答@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();
}
您可以将标准分解为三个较小标准的析取:
-1:这还不能回答问题。OP需要从一个日期到另一个日期的所有记录。在仔细阅读问题后,我更正了我以前的回答。谢谢你,hintIt仍然没有回答这个问题-他想用标准或限制来解决这个问题,而不是HQL。尽管如此,考虑到他不会继续以排除HQL查询的方式使用这些条件,他为什么要声明这一限制并不明显。您的解决方案比his更简洁、更高效、更易于阅读和维护。它可以工作,只需要很少的语法更改。这里是生成的SQL:从CommissionSummary this中选择this.*,其中((this..year=2011和this..month>=11)或(this..year>2011和this..year
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))
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))
);