如何使用 Hibernate Criteria API 获取最近 24 小时的提交数量
How to get number of submission of last 24 hours using Hibernate Criteria API
谁能告诉我如何找出 24 小时内提交了多少表单?
我的数据库中有提交时间。
在仪表板中,我需要打印过去 24 小时的提交数量。
我的代码
@Override
public long last24Hours() {
Date yesterday = new Date(System.currentTimeMillis() - 1000L * 60L * 60L * 24L);
CriteriaBuilder cb = sessionFactory.getCurrentSession().getCriteriaBuilder();
CriteriaQuery<Long> query = cb.createQuery(Long.class);
Root<FormSubmission> formSubmission = query.from(FormSubmission.class);
query.select(cb.count(formSubmission));
query.where(cb.greaterThanOrEqualTo(formSubmission.get("submitted"), yesterday));
Query<Long> q = sessionFactory.getCurrentSession().createQuery(query);
return q.getFirstResult();
}
但是返回0,不知道为什么
//这是有效的
@Override
public long last24Hours() {
Date yesterday = new Date(System.currentTimeMillis() - 1000L * 60L * 60L * 24L);
CriteriaBuilder cb = sessionFactory.getCurrentSession().getCriteriaBuilder();
CriteriaQuery<Long> query = cb.createQuery(Long.class);
Root<FlowSubmission> flowSubmission = query.from(FlowSubmission.class);
query.select(cb.count(flowSubmission));
query.where(cb.greaterThanOrEqualTo(flowSubmission.get("submitted"), yesterday));
Query<Long> q = sessionFactory.getCurrentSession().createQuery(query);
return q.uniqueResult();
}