public static Hits getLastHits() { // EntityManager entityManager = getEntityManager(); Hits last = null; try { // last = entityManager.createQuery("from Hits h order by id desc", Hits.class) // .setFirstResult(0).setMaxResults(1).getSingleResult(); last = (Hits) Q("from Hits h order by id desc") .setFirstResult(0) .setMaxResults(1) .getSingleResult(); } catch (Exception e) { last = null; } if (last != null) { return last; } else { last = new Hits(); last.setDate(new Date()); // entityManager.persist(last); getEntityManager().persist(last); return last; } }
// @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = // Exception.class) public static void addHits(boolean isLogin) { if (lastHits == null) lastHits = getLastHits(); Calendar now = Calendar.getInstance(); Calendar last = Calendar.getInstance(); last.setTime(lastHits.getDate()); l.info("last time: {}", last.getTime()); l.info("this time: {}", now.getTime()); if (now.get(Calendar.DATE) != last.get(Calendar.DATE)) { lastHits = new Hits(); lastHits.setDate(now.getTime()); lastHits.setHits(1); setUniqueHits(lastHits, isLogin); lastHits.setUniqueHits(1); getEntityManager().persist(lastHits); } else { lastHits.setHits(lastHits.getHits() + 1); setUniqueHits(lastHits, isLogin); getEntityManager().merge(lastHits); } }
public static void setUniqueHits(Hits hits, boolean isLogin) { if (!isLogin) hits.setUniqueHits(hits.getUniqueHits() + 1); }