【发布时间】:2018-03-15 13:03:30
【问题描述】:
我正在尝试使用 Criteria API 获取我的数据库中两个字段之间日期的记录。我需要构建一个如下所示的查询:
select tran_date_bal from eod_acct_bal_table where acid = <MyAcid> and <BalanceDate> between eod_date and end_eod_date;
由于提供的between 不允许两个属性,因此我一直在获取日期标准。这是我目前所拥有的:
private static double getEndOfDayBalanceAsAtDate(String foracid, Date BalanceDate) {
double balance = 0;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(EodAcctBalTable.class);
cr.add(Restrictions.eq("acid", getAcid(foracid)));
cr.add(Restrictions.between());//****Stuck Here***********
balance = (Double) cr.setProjection(Projections.property("tranDateBal")).uniqueResult();
tx.commit();
} catch (Exception asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return balance;
}
如何在坚持标准的同时生成查询?
【问题讨论】:
-
你试过
(Restrictions.between("yourDate", DateBegin, DateEnd))吗? -
YourDate不能作为between的第一个参数,因为这是在 EOD_ACCT_BAL_TABLE 中寻找字段属性 -
和
BalanceDate是 EOD_ACCT_BAL_TABLE 中的字段属性所以! -
否
BalanceDate是日期类型的输入参数
标签: java sql hibernate oracle11g