簡體   English   中英

Hibernate 標准中的日期之間的限制

[英]Restrictions Between for Date in Hibernate Criteria

您好,我在示例中使用 hibernate。對於 bean 表審計試驗,我想在包含上限和下限的日期范圍之間獲取審計試驗。 我的代碼如下

Criteria criteria = session.createCriteria(AuditTrail.class);

criteria.add(Restrictions.between("auditDate", sDate, eDate));

我的開始日期是25/11/2010 和結束日期是25/05/2011 。但它只給出24/05/2011的結果。它不執行包容性搜索。任何其他方式來做到這一點。 我正在使用 SQL 服務器。

我假設您的 auditDate 實際上是一個時間戳。 如果是這種情況,那么這是正常的,因為 25/05/2011 表示 25/05/2011 的 0 點(早上)。 因此,當然,在 2011 年 5 月 25 日具有審計時間戳的每一行都在早上 0 點之后。

我會在您的結束日期上增加 1 天,並使用auditDate >= sDate and auditDate < eDate

criteria.add(Restrictions.ge("auditDate", sDate)); 
criteria.add(Restrictions.lt("auditDate", eDate));

criteria.add(Restrictions.between("DATE(auditDate)", sDate, eDate)); 使用它來忽略從日期開始的時間。

 criteria.add(Restrictions.ge("fromDate", DateUtil.persianToGregorian(currentDate)));
 criteria.add(Restrictions.le("toDate",  DateUtil.persianToGregorian(currentDate)));

 return criteria.list();

再增加一天到結束日期

 @Autowired
 private SessionFactory sessionFactory;

 String startDate = "2019-07-31 ";
 String endDate = "2019-08-24 ";

 Date fromDate = format.parse(fromDate);

 /* Add one more day to the end date*/

 Date to =format.parse(endDate);
 Calendar today = Calendar.getInstance();
 today.setTime(dateto);
 today.add(Calendar.DAY_OF_YEAR, 1);

 Date toDate= format.parse(format.format(today.getTime()));

 Criteria crit = sessionFactory.getCurrentSession().createCriteria(model.class);
 crit.add(Restrictions.between("dateFieldName", fromDate, toDate));
 List result = crit.list();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM