Example usage for org.hibernate Criteria setProjection

List of usage examples for org.hibernate Criteria setProjection

Introduction

In this page you can find the example usage for org.hibernate Criteria setProjection.

Prototype

public Criteria setProjection(Projection projection);

Source Link

Document

Used to specify that the query results will be a projection (scalar in nature).

Usage

From source file:be.redlab.examples.databasetesting.common.AbstractJpaDao.java

License:Apache License

protected long countByCriteria(final Criterion... criterion) {
    Session session = (Session) getEntityManager().getDelegate();
    Criteria crit = session.createCriteria(getEntityClass());
    crit.setProjection(Projections.rowCount());

    for (final Criterion c : criterion) {
        crit.add(c);//from w  ww.  jav  a  2 s.c  om
    }

    return (Long) crit.list().get(0);
}

From source file:br.al.contractmanager.dao.UsuarioDao.java

@Transactional(readOnly = true)
public int count() {
    Session session = sessionFactory.getCurrentSession();
    try {//from   w  ww.ja  va2  s  .  c  o m
        Criteria criteria = session.createCriteria(Usuario.class);
        criteria.setProjection(Projections.distinct(Projections.id()));
        return ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
    } catch (HibernateException e) {
        System.out.println(e.getMessage());
        return 0;
    }
}

From source file:br.al.contractmanager.dao.UsuarioDao.java

@Transactional(readOnly = true)
public int count(String termo) {
    Session session = sessionFactory.getCurrentSession();
    try {//from   w  w  w.j a va 2s . c  om
        Criteria criteria = session.createCriteria(Usuario.class);
        criteria.add(Restrictions.or(Restrictions.eq("matricula", termo),
                Restrictions.like("nomeUsuario", termo, MatchMode.ANYWHERE).ignoreCase(),
                Restrictions.like("nomeCompleto", termo, MatchMode.ANYWHERE).ignoreCase()))
                .setProjection(Projections.distinct(Projections.id()));
        return ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
    } catch (HibernateException e) {
        System.out.println(e.getMessage());
        return 0;
    }
}

From source file:br.com.arsmachina.dao.hibernate.ReadableDAOImpl.java

License:Apache License

/**
 * @see br.com.arsmachina.dao.ReadableDAO#countAll()
 *//*from  www. j ava  2s  . co m*/
public int countAll() {

    final Criteria criteria = createCriteria();

    criteria.setProjection(Projections.rowCount());

    return (Integer) criteria.uniqueResult();

}

From source file:br.com.ateliens.repository.ClientesFiltrados.java

public int quantidadeFiltrados(FiltroCliente filtro) {
    Criteria criteria = criarCriteriaParaFiltro(filtro);

    criteria.setProjection(Projections.rowCount());

    return ((Number) criteria.uniqueResult()).intValue();
}

From source file:br.com.gerenciapessoal.repository.Lancamentos.java

@SuppressWarnings("UnusedAssignment")
public Map<Date, BigDecimal> valoresTotaisPorData(Integer numeroDeDias, Conta conta) {
    Session session = manager.unwrap(Session.class);

    Calendar dataInicial = Calendar.getInstance();
    dataInicial = DateUtils.truncate(dataInicial, Calendar.DAY_OF_MONTH);
    dataInicial.add(Calendar.DAY_OF_MONTH, numeroDeDias * -1);

    Map<Date, BigDecimal> resultado = criarMapaVazio(numeroDeDias, dataInicial);

    Criteria criteria = session.createCriteria(Lancamento.class).createAlias("conta", "c");

    criteria.setProjection(Projections.projectionList()
            .add(Projections.sqlGroupProjection("date(data_emissao) as data", "date(data_emissao)",
                    new String[] { "data" }, new Type[] { StandardBasicTypes.DATE }))
            .add(Projections.sum("valorLanca").as("valor")))
            .add(Restrictions.ge("dataEmissao", dataInicial.getTime()));

    if (conta != null) {
        criteria.add(Restrictions.eq("c.id", conta.getId()));
    }/*from   w w w. j  a va  2s . c o m*/
    List<DataValor> valoresPorData = criteria.setResultTransformer(Transformers.aliasToBean(DataValor.class))
            .list();

    for (DataValor dataValor : valoresPorData) {
        resultado.put(dataValor.getData(), dataValor.getValor());
    }

    return resultado;
}

From source file:br.com.hrstatus.dao.impl.BancoDadosDAO.java

License:Open Source License

public int countMysql() {

    log.fine("[ " + userInfo.getLoggedUsername() + " ] countMysql()");

    final Criteria criteria = session().createCriteria(BancoDados.class);
    criteria.add(Restrictions.eq("vendor", "MYSQL"));
    criteria.setProjection(Projections.rowCount());
    final int count = ((Long) criteria.uniqueResult()).intValue();
    log.fine("[ " + userInfo.getLoggedUsername() + " ] countMysql() -> Found " + count + " Mysql Databases.");
    return count;
}

From source file:br.com.hrstatus.dao.impl.BancoDadosDAO.java

License:Open Source License

public int countOracle() {

    log.fine("[ " + userInfo.getLoggedUsername() + " ] countOracle()");

    final Criteria criteria = session().createCriteria(BancoDados.class);
    criteria.add(Restrictions.eq("vendor", "ORACLE"));
    criteria.setProjection(Projections.rowCount());
    final int count = ((Long) criteria.uniqueResult()).intValue();
    log.fine(/*from   w ww  .  j a  va 2  s. com*/
            "[ " + userInfo.getLoggedUsername() + " ] countOracle() -> Found " + count + " Oracle Databases..");
    return count;
}

From source file:br.com.hrstatus.dao.impl.BancoDadosDAO.java

License:Open Source License

public int countPostgre() {

    log.fine("[ " + userInfo.getLoggedUsername() + " ] countPostgre()");

    final Criteria criteria = session().createCriteria(BancoDados.class);
    criteria.add(Restrictions.eq("vendor", "POSTGRESQL"));
    criteria.setProjection(Projections.rowCount());
    final int count = ((Long) criteria.uniqueResult()).intValue();
    log.fine("[ " + userInfo.getLoggedUsername() + " ] countPostgre() -> Found " + count
            + " Postgre Databases.");
    return count;
}

From source file:br.com.hrstatus.dao.impl.BancoDadosDAO.java

License:Open Source License

public int countSqlServer() {

    log.fine("[ " + userInfo.getLoggedUsername() + " ] countSqlServer()");

    final Criteria criteria = session().createCriteria(BancoDados.class);
    criteria.add(Restrictions.eq("vendor", "SQLSERVER"));
    criteria.setProjection(Projections.rowCount());
    final int count = ((Long) criteria.uniqueResult()).intValue();
    log.fine("[ " + userInfo.getLoggedUsername() + " ] countSqlServer() -> Found " + count
            + " SqlServer Databases.");
    return count;
}