List of usage examples for org.hibernate.criterion Projections count
public static CountProjection count(String propertyName)
From source file:postgreHibernateClient.postgreHibClient.java
License:Open Source License
@Override public HashMap<String, String> getInitialStats() { HashMap<String, String> stats = new HashMap<String, String>(); // session = sessionFactory.openSession(); try {/*from w ww . ja v a2 s. co m*/ tx = session.beginTransaction(); Criteria c = session.createCriteria(USERS.class).setProjection(Projections.count("userid")); Long usercount = (Long) c.list().get(0); usercount = usercount > 0 ? usercount : 0; stats.put("usercount", Long.toString(usercount)); Criteria c1 = session.createCriteria(USERS.class).setProjection(Projections.min("userid")); String offset = (String) c1.list().get(0); //get resources per user USERS u = new USERS(); u = (USERS) session.get(USERS.class, offset); stats.put("avgfriendsperuser", Integer.toString(u.getConfFriendCnt())); stats.put("resourcesperuser", Integer.toString(u.getResCnt())); stats.put("avgpendingperuser", Integer.toString(u.getPendFriendCnt())); } catch (Exception e) { e.printStackTrace(); } finally { // session.close(); } return stats; }
From source file:ro.cs.om.model.dao.impl.DaoDepartmentImpl.java
License:Open Source License
/** * Gets the number of departments for an organisation (without the fake departments) * /* w ww.j a v a2 s.c o m*/ * @author Adelina * @parm organisationId * @return number of departments from an organisation */ @SuppressWarnings("unchecked") public int getCountDepartments(Integer organisationId) { logger.debug("getCountDepartments - START"); DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.departmentEntity); dc.setProjection(Projections.count("departmentId")); dc.createCriteria("organisation").add(Restrictions.eq("organisationId", organisationId)); dc.add(Restrictions.ne("status", (byte) 2)); List departaments = getHibernateTemplate().findByCriteria(dc); logger.debug("getCountDepartments - END - ".concat(String.valueOf(departaments.size()))); if (departaments != null) { int count = ((Integer) departaments.get(0)).intValue(); logger.debug("count " + count); return count; } else { return 0; } }
From source file:to.etc.domui.hibernate.model.CriteriaCreatingVisitor.java
License:Open Source License
@Override public void visitPropertySelection(QPropertySelection n) throws Exception { String name = parseSubcriteria(n.getProperty()); switch (n.getFunction()) { default:/*from ww w. j ava2 s .c om*/ throw new IllegalStateException("Unexpected selection item function: " + n.getFunction()); case AVG: m_lastProj = Projections.avg(name); break; case MAX: m_lastProj = Projections.max(name); break; case MIN: m_lastProj = Projections.min(name); break; case SUM: m_lastProj = Projections.sum(name); break; case COUNT: m_lastProj = Projections.count(name); break; case COUNT_DISTINCT: m_lastProj = Projections.countDistinct(name); break; case ID: m_lastProj = Projections.id(); break; case PROPERTY: m_lastProj = Projections.groupProperty(name); break; case ROWCOUNT: m_lastProj = Projections.rowCount(); break; case DISTINCT: m_lastProj = Projections.distinct(Projections.property(name)); break; } }