List of usage examples for org.hibernate.criterion Projections rowCount
public static Projection rowCount()
From source file:cn.dayuanzi.dao.BaseDao.java
License:Apache License
/** * ??// w w w . j a va 2s .com * @param criterions * @return */ public long count(final Criterion... criterions) { Criteria criteria = createCriteria(criterions); criteria.setProjection(Projections.rowCount()); return (long) criteria.uniqueResult(); }
From source file:cn.hxh.springside.orm.hibernate.HibernateDao.java
License:Apache License
/** * countCriteria./*from w w w . j av a 2 s .c o m*/ */ protected long countCriteriaResult(final Criteria c) { CriteriaImpl impl = (CriteriaImpl) c; // Projection?ResultTransformer?OrderBy??,??Count? Projection projection = impl.getProjection(); ResultTransformer transformer = impl.getResultTransformer(); List<CriteriaImpl.OrderEntry> orderEntries = null; try { orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries"); ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList()); } catch (Exception e) { logger.error("??:{}", e.getMessage()); } // Count Long totalCountObject = (Long) c.setProjection(Projections.rowCount()).uniqueResult(); long totalCount = (totalCountObject != null) ? totalCountObject : 0; // ?Projection,ResultTransformerOrderBy?? c.setProjection(projection); if (projection == null) { c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); } if (transformer != null) { c.setResultTransformer(transformer); } try { ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries); } catch (Exception e) { logger.error("??:{}", e.getMessage()); } return totalCount; }
From source file:cn.lhfei.airqa.dao.support.Hibernate4DaoSupport.java
License:Apache License
@Override public Page<E> findPageByCriteria(Criterion criterion, Page<E> page) { Criteria criteria = getCurrentSession().createCriteria(entityClass); int count = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue(); criteria.setProjection(null);//from ww w.ja va 2s . c o m if (criterion != null) { criteria.add(criterion); } page.setTotalCount(count); int startCount = (page.getCurPage() - 1) * page.getPageSize(); int endCount = startCount + page.getPageSize(); criteria.setFirstResult(startCount); criteria.setMaxResults(endCount); page.setResult(criteria.list()); return page; }
From source file:cn.lhfei.fu.orm.persistence.impl.HomeworkBaseDAOImpl.java
License:Apache License
@Override public SearchAndCountModel<HomeworkBase> getPageAndCount(HomeworkBaseModel homeworkModel) { SearchAndCountModel<HomeworkBase> resultModel = new SearchAndCountModel<HomeworkBase>(); List<HomeworkBase> list = new ArrayList<HomeworkBase>(); //String sql = "From HomeworkBase as hb left join hb.archives with hb.className = :className "; Session session = null;/*from w w w . j av a2 s.c o m*/ int total; try { session = getSessionFactory().getCurrentSession(); } catch (HibernateException e) { session = getSessionFactory().openSession(); } Criteria criteria = session.createCriteria(HomeworkBase.class, "homeworkBase"); for (SimpleExpression exp : homeworkModel.wrapperFilter()) { criteria.add(exp); } //criteria.setFetchMode("homeworkBase.archives", FetchMode.JOIN); criteria.createCriteria("homeworkBase.archives", JoinType.LEFT_OUTER_JOIN); // criteria.createAlias("homeworkBase.archives", "archives"); total = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue(); criteria.setProjection(null); list = criteria.setFirstResult(homeworkModel.getFirst()).setMaxResults(homeworkModel.getPageSize()) .setCacheable(true).list(); resultModel.setTotal(total); resultModel.setRows(list); return resultModel; }
From source file:cn.net.withub.demo.bootsec.hello.dao.impl.BaseDAOImpl.java
@Override public int count(DetachedCriteria criteria) { Session session = currentSession();/* www . j a v a 2s. co m*/ return ((Number) criteria.getExecutableCriteria(session).setProjection(Projections.rowCount())).intValue(); }
From source file:cn.newtouch.util.hibernate.HibernateDao.java
License:Apache License
/** * countCriteria.//from w ww . ja va 2 s . c o m */ @SuppressWarnings("unchecked") protected long countCriteriaResult(final Criteria c) { CriteriaImpl impl = (CriteriaImpl) c; // Projection?ResultTransformer?OrderBy??,??Count? Projection projection = impl.getProjection(); ResultTransformer transformer = impl.getResultTransformer(); List<CriteriaImpl.OrderEntry> orderEntries = null; try { orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries"); ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList()); } catch (Exception e) { logger.error("??:{}", e.getMessage()); } // Count Long totalCountObject = (Long) c.setProjection(Projections.rowCount()).uniqueResult(); long totalCount = (totalCountObject != null) ? totalCountObject : 0; // ?Projection,ResultTransformerOrderBy?? c.setProjection(projection); if (projection == null) { c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); } if (transformer != null) { c.setResultTransformer(transformer); } try { ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries); } catch (Exception e) { logger.error("??:{}", e.getMessage()); } return totalCount; }
From source file:cn.trymore.core.dao.impl.DAOGenericImpl.java
License:Open Source License
@SuppressWarnings("unchecked") public PaginationSupport<T> findPageByCriteria(final DetachedCriteria criteria, final int pageSize, final int startIndex, final boolean dataFilter) throws DAOException { if (dataFilter && UtilString.isNotEmpty(this.getQueryFilter())) { criteria.add(Restrictions.sqlRestriction(this.getQueryFilter())); this.setQueryFilter(null); }//from ww w. j a v a 2 s. com return (PaginationSupport<T>) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Criteria execCriteria = criteria.getExecutableCriteria(session); int rowCount = ((Integer) execCriteria.setProjection(Projections.rowCount()).uniqueResult()) .intValue(); execCriteria.setProjection(null); execCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); //execCriteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); execCriteria.setFirstResult(startIndex); if (pageSize > 0) { execCriteria.setMaxResults(pageSize); } else { execCriteria.setMaxResults(rowCount); } List<T> items = execCriteria.list(); return rowCount > 0 ? new PaginationSupport<T>(items, rowCount, startIndex, pageSize > 0 ? pageSize : rowCount) : null; } }); }
From source file:co.com.codesoftware.logic.productos.FacturaCompraLogic.java
/** * metodo que trae el consecutivo para insertar en la factura de compra * * @return//from w w w . ja v a 2 s . com */ public Integer selectMaxFactura() { Long resultado = new Long(1); try { initOperation(); resultado = (Long) sesion.createCriteria(FacturaCompraEntity.class) .setProjection(Projections.rowCount()).uniqueResult() + 1; close(); } catch (Exception e) { e.printStackTrace(); } return resultado.intValue(); }
From source file:co.com.codesoftware.logic.productos.FacturaCompraLogic.java
/** * metodo que consulta el id de las imagenes * * @return//w ww .j av a 2 s . c o m */ public Integer selectMaxImagen() { Long resultado = new Long(1); try { initOperation(); resultado = (Long) sesion.createCriteria(ImagenesFacCompraEntity.class) .setProjection(Projections.rowCount()).uniqueResult() + 1; } catch (Exception e) { e.printStackTrace(); } return resultado.intValue(); }
From source file:co.com.codesoftware.logic.productos.FacturaCompraLogic.java
/** * metodo que consulta el id de la tabla producto * * @return//w w w . j a v a 2s .com */ public Integer selectMaxProd() { Long resultado = new Long(1); try { initOperation(); resultado = (Long) sesion.createCriteria(ProductoFacCompraEntity.class) .setProjection(Projections.rowCount()).uniqueResult() + 1; close(); } catch (Exception e) { e.printStackTrace(); } return resultado.intValue(); }