Home > Net >  How to get number of submission of last 24 hours using Hibernate Criteria API
How to get number of submission of last 24 hours using Hibernate Criteria API

Time:12-18

Can somebody tell me how to find out how many forms were submitted in 24 hours?

I have the submitted time in my database.

enter image description here

In the dashboard, I need to print the number of submissions for the last 24 hours.

My code

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

    
}

But it returning 0, don't know why

CodePudding user response:

//this is working

@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();
}
  • Related