I have it:
public Map<Day,Integer> getUniqueLogins(long fromTime, long toTime) {
EntityManager em = emf.createEntityManager();
try {
Map<Day,Integer> resultMap = new ...;
for (Day day : daysInPeriod(fromTime, toTime)) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Long> q = cb.createQuery(Long.class);
Root<UserSession> userSess = q.from(UserSession.class);
q.select(cb.countDistinct(userSess.<Long>get("userId")));
q.where(cb.between(userSess.<Date>get("loginTime"), day.startDate(), day.endDate()));
long result = em.createQuery(q).getSingleResult();
resultMap.put(day, (int) result);
}
return resultMap;
} finally {
em.close();
}
}
Fulfills a request for each day for a certain period (the period is about a month).
Can I get this specific data in one query? I use Hibernate / MySQL, but I would prefer not to use any non-standard functions.
source
share