Example usage for org.hibernate.criterion Projections rowCount

List of usage examples for org.hibernate.criterion Projections rowCount

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections rowCount.

Prototype

public static Projection rowCount() 

Source Link

Document

The query row count, ie.

Usage

From source file:com.it355.filip.dao.impl.UsersDaoImpl.java

@Override
public int getCount() {
    Session session = this.sessionFactory.getCurrentSession();
    return (int) (Number) session.createCriteria("account").setProjection(Projections.rowCount())
            .uniqueResult();//from ww w.j a  va 2s.c o m

    /*String sql = "SELECT COUNT(*) FROM account"; 
    int count = jdbcTemplate.queryForObject(sql, Integer.class); 
    return count;    */
}

From source file:com.it355.filip.dao.impl.WatchDaoImpl.java

@Override
public int getCount() {
    Session session = this.sessionFactory.getCurrentSession();
    return (int) (Number) session.createCriteria("watch").setProjection(Projections.rowCount()).uniqueResult();
    /*//from w  w w .  j av a 2 s  .  c o m
    String sql = "SELECT COUNT(*) FROM watch"; 
    int count = jdbcTemplate.queryForObject(sql, Integer.class); 
    return count; */
}

From source file:com.jacobheric.youbrew.dao.impl.ExpenseDAOImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Expense> search(ExpenseCriteria expenseCriteria) {

    Criteria c = this.getSessionFactory().getCurrentSession().createCriteria(Expense.class);

    ////from  w w  w  .  ja  v a  2 s .c o m
    //Property restrictions
    if (expenseCriteria.getQuery() != null) {
        Disjunction d = Restrictions.disjunction();

        d.add(Restrictions.like("name", expenseCriteria.getQuery().trim(), MatchMode.ANYWHERE));
        d.add(Restrictions.like("tasteNotes", expenseCriteria.getQuery().trim(), MatchMode.ANYWHERE));
        d.add(Restrictions.like("brewNotes", expenseCriteria.getQuery().trim(), MatchMode.ANYWHERE));

        c.add(d);
    }

    //
    //Determine the total before limiting (useful for paging)
    c.setProjection(Projections.rowCount());
    expenseCriteria.setTotal(((Integer) c.uniqueResult()).intValue());
    c.setProjection(null);
    c.setResultTransformer(Criteria.ROOT_ENTITY);

    //
    //Start & limit grid page restrictions
    if (expenseCriteria.getStart() != null) {
        c.setFirstResult(expenseCriteria.getStart());
    }

    if (expenseCriteria.getLimit() != null) {
        c.setMaxResults(expenseCriteria.getLimit());
    }

    return c.list();
}

From source file:com.jacobheric.youbrew.dao.impl.RecipeDAOImpl.java

License:Open Source License

/**
 * @param recipeCriteria - custom criteria object
 * @return list of recipe criteria/*from ww w  .ja v  a2 s  . c om*/
 */
@SuppressWarnings("unchecked")
public List<Recipe> search(RecipeCriteria recipeCriteria) {

    Criteria c = this.getSessionFactory().getCurrentSession().createCriteria(Recipe.class);

    //
    //Property restrictions
    if (recipeCriteria.getQuery() != null) {
        Disjunction d = Restrictions.disjunction();

        d.add(Restrictions.like("name", recipeCriteria.getQuery().trim(), MatchMode.ANYWHERE));
        d.add(Restrictions.like("tasteNotes", recipeCriteria.getQuery().trim(), MatchMode.ANYWHERE));
        d.add(Restrictions.like("brewNotes", recipeCriteria.getQuery().trim(), MatchMode.ANYWHERE));

        c.add(d);
    }

    //
    //Determine the total before limiting (useful for paging)
    c.setProjection(Projections.rowCount());
    recipeCriteria.setTotal(((Integer) c.uniqueResult()).intValue());
    c.setProjection(null);
    c.setResultTransformer(Criteria.ROOT_ENTITY);

    //
    //Start & limit grid page restrictions
    if (recipeCriteria.getStart() != null) {
        c.setFirstResult(recipeCriteria.getStart());
    }

    if (recipeCriteria.getLimit() != null) {
        c.setMaxResults(recipeCriteria.getLimit());
    }

    return c.list();
}

From source file:com.jaspersoft.jasperserver.api.logging.access.service.impl.AccessServiceImpl.java

License:Open Source License

public int getAccessEventsCount() {
    DetachedCriteria criteria = DetachedCriteria
            .forClass(persistentClassFactory.getImplementationClass(AccessEvent.class));
    criteria.setProjection(Projections.rowCount());
    return (Integer) getHibernateTemplate().findByCriteria(criteria).get(0);
}

From source file:com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.HibernateRepositoryServiceImpl.java

License:Open Source License

public boolean resourceExists(ExecutionContext executionContext, FilterCriteria filterCriteria) {
    DetachedCriteria criteria = translateFilterToCriteria(filterCriteria);
    boolean exists;
    if (criteria == null) {
        exists = false;/*from   w w w  . ja  v  a2s  .c o m*/
    } else {
        criteria.setProjection(Projections.rowCount());
        criteria.getExecutableCriteria(getSession()).setCacheable(true);
        List countList = getHibernateTemplate().findByCriteria(criteria);
        int count = ((Integer) countList.get(0)).intValue();
        exists = count > 0;
    }
    return exists;
}

From source file:com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.HibernateRepositoryServiceImpl.java

License:Open Source License

protected boolean resourceExists(RepoFolder folder, String name, Class resourceType) {
    Class persistentClass = resourcePersistentClass(resourceType);
    DetachedCriteria criteria = resourceNameCriteria(persistentClass, folder, name);
    criteria.setProjection(Projections.rowCount());
    criteria.getExecutableCriteria(getSession()).setCacheable(true);
    List countList = getHibernateTemplate().findByCriteria(criteria);
    int count = ((Integer) countList.get(0)).intValue();
    return count > 0;
}

From source file:com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.HibernateRepositoryServiceImpl.java

License:Open Source License

@Transactional(propagation = Propagation.REQUIRED)
public int getFoldersCount(final String parentURI) {
    return (Integer) executeCallback(new DaoCallback() {
        public Object execute() {
            DetachedCriteria criteria = DetachedCriteria.forClass(RepoFolder.class);
            criteria.add(Restrictions.eq("hidden", Boolean.FALSE));
            criteria.setProjection(Projections.rowCount());
            if (parentURI != null && !Folder.SEPARATOR.equals(parentURI)) {
                criteria.add(Restrictions.or(
                        (parentURI.contains("%")) ? Restrictions.eq("URI", parentURI)
                                : Restrictions.like("URI", parentURI),
                        Restrictions.like("URI", parentURI + "/%")));
            }/*w  w  w.j a v  a2 s .  c  o  m*/
            return new BasicTransformer().transformToCount(getHibernateTemplate().findByCriteria(criteria));
        }
    });
}

From source file:com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.HibernateRepositoryServiceImpl.java

License:Open Source License

@Transactional(propagation = Propagation.REQUIRED)
public int getResourcesCount(ExecutionContext context, SearchCriteriaFactory searchCriteriaFactory,
        List<SearchFilter> filters, SearchSorter sorter, TransformerFactory transformerFactory) {
    if (transformerFactory == null) {
        throw new IllegalArgumentException("Transformer factory is null.");
    }//  w w  w . jav  a 2 s. c o m

    SearchCriteria criteria;
    if (queryModificationEvaluator.useFullResource(context)) {
        criteria = searchCriteriaFactory.newFactory(Resource.class.getName()).create(context, filters);
        criteria.setProjection(Projections.countDistinct("id"));
    } else {
        criteria = searchCriteriaFactory.create(context, filters);
        criteria.setProjection(Projections.rowCount());
    }

    List resourceList = getHibernateTemplate().findByCriteria(criteria);

    ResultTransformer transformer = transformerFactory.createTransformer(filters, sorter);
    if (transformer != null) {
        return transformer.transformToCount(resourceList);
    } else {
        throw new IllegalArgumentException("Result transformer is null.");
    }
}

From source file:com.jaspersoft.jasperserver.api.metadata.data.snapshot.hibernate.HibernateDataSnapshotService.java

License:Open Source License

@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public boolean matchesVersion(ExecutionContext context, final long snapshotId, final int version) {
    return getHibernateTemplate().execute(new HibernateCallback<Boolean>() {
        public Boolean doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(PersistentDataSnapshot.class);
            criteria.add(Restrictions.idEq(snapshotId));
            criteria.add(Restrictions.eq("version", version));
            criteria.setProjection(Projections.rowCount());
            Number count = (Number) criteria.uniqueResult();

            if (log.isDebugEnabled()) {
                log.debug("version check for " + snapshotId + " and " + version + " returned " + count);
            }/*from  ww  w.  j  av  a2  s  . co  m*/

            return count.intValue() > 0;
        }
    });
}