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:br.com.webbudget.domain.model.repository.financial.FixedMovementRepository.java

License:Open Source License

/**
 *
 * @param filter/*from w  w w . j  a va  2 s.c o  m*/
 * @param pageRequest
 * @return
 */
@Override
public Page<FixedMovement> listByFilter(String filter, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    final List<Criterion> criterions = new ArrayList<>();

    // filtramos
    if (filter != null && !filter.isEmpty()) {

        criterions.add(Restrictions.ilike("description", "%" + filter + "%"));
        criterions.add(Restrictions.ilike("identification", "%" + filter + "%"));

        // se conseguir castar para bigdecimal trata como um filtro
        try {
            criterions.add(Restrictions.eq("value", new BigDecimal(filter)));
        } catch (NumberFormatException ex) {
        }
    }

    criteria.add(Restrictions.or(criterions.toArray(new Criterion[] {})));

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.financial.LaunchRepository.java

License:Open Source License

/**
 * //from  www.  j a va 2  s  .  co m
 * @param fixedMovement
 * @return 
 */
@Override
public Long countByFixedMovement(FixedMovement fixedMovement) {

    final Criteria criteria = this.createCriteria();

    criteria.createAlias("fixedMovement", "fm");
    criteria.add(Restrictions.eq("fm.id", fixedMovement.getId()));

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    return (Long) criteria.uniqueResult();
}

From source file:br.com.webbudget.domain.model.repository.financial.LaunchRepository.java

License:Open Source License

/**
 * //from   w  w  w  .jav  a2  s  . c  o m
 * @param fixedMovement
 * @param pageRequest
 * @return 
 */
@Override
public Page<Launch> listByFixedMovement(FixedMovement fixedMovement, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    criteria.createAlias("fixedMovement", "fm");
    criteria.add(Restrictions.eq("fm.id", fixedMovement.getId()));

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.financial.MovementRepository.java

License:Open Source License

/**
 *
 * @param filter/*  ww  w .ja  v  a2 s.  co m*/
 * @param pageRequest
 * @return
 */
@Override
public Page<Movement> listByFilter(MovementFilter filter, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    final List<Criterion> criterions = new ArrayList<>();

    criteria.createAlias("contact", "co", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("apportionments", "ap");
    criteria.createAlias("ap.movementClass", "mc");
    criteria.createAlias("ap.costCenter", "cc");
    criteria.createAlias("financialPeriod", "fp");

    // montramos os criterios de filtragem geral
    if (filter.hasCriteria()) {
        criterions.add(Restrictions.eq("code", filter.getCriteria()));
        criterions.add(Restrictions.ilike("description", "%" + filter.getCriteria() + "%"));
        criterions.add(Restrictions.ilike("mc.name", "%" + filter.getCriteria() + "%"));
        criterions.add(Restrictions.ilike("cc.name", "%" + filter.getCriteria() + "%"));
        criterions.add(Restrictions.ilike("co.name", "%" + filter.getCriteria() + "%"));

        // se conseguir castar para bigdecimal trata como um filtro
        try {
            criterions.add(Restrictions.eq("value", filter.criteriaToBigDecimal()));
        } catch (ParseException ex) {
        }
    }

    criteria.add(Restrictions.or(criterions.toArray(new Criterion[] {})));
    criteria.add(Restrictions.and(filter.getCustomFilters()));

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    // aplica o multisort dos campos
    if (pageRequest.isMultiSort()) {
        pageRequest.getMultiSortFields().stream().forEach(field -> {
            if (field.getDirection() == SortDirection.ASC) {
                criteria.addOrder(Order.asc(field.getSortField()));
            } else if (field.getDirection() == SortDirection.DESC) {
                criteria.addOrder(Order.desc(field.getSortField()));
            }
        });
    } else if (pageRequest.getSortDirection() == SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.financial.MovementRepository.java

License:Open Source License

/**
 *
 * @param period/* w w w  .  j ava  2  s.c  om*/
 * @param movementClass
 * @return
 */
@Override
public BigDecimal countTotalByPeriodAndMovementClass(FinancialPeriod period, MovementClass movementClass) {

    final Criteria criteria = this.getSession().createCriteria(this.getPersistentClass());

    criteria.createAlias("financialPeriod", "fp");
    criteria.add(Restrictions.eq("fp.id", period.getId()));

    criteria.createAlias("apportionments", "ap");
    criteria.createAlias("ap.movementClass", "mc");
    criteria.add(Restrictions.eq("mc.id", movementClass.getId()));

    criteria.setProjection(Projections.sum("value"));

    return (BigDecimal) criteria.uniqueResult();
}

From source file:br.com.webbudget.domain.model.repository.logbook.RefuelingRepository.java

License:Open Source License

/**
 * /*  w  w w. jav  a  2s.c o m*/
 * @param refueling
 * @return 
 */
@Override
public boolean isLast(Refueling refueling) {

    Criteria criteria = this.createCriteria();

    criteria.createAlias("vehicle", "ve");
    criteria.add(Restrictions.eq("ve.id", refueling.getVehicle().getId()));
    criteria.setProjection(Projections.max("id"));

    Object maxId = criteria.uniqueResult();

    if (maxId != null) {
        return refueling.getId().equals((Long) maxId);
    }
    return true;
}

From source file:br.com.webbudget.domain.model.repository.logbook.RefuelingRepository.java

License:Open Source License

/**
 *
 * @return//w w  w.ja  va 2s  .c  o m
 */
@Override
public int findLastOdometerForVehicle(Vehicle vehicle) {

    Criteria criteria = this.createCriteria();

    criteria.createAlias("vehicle", "ve");
    criteria.add(Restrictions.eq("ve.id", vehicle.getId()));
    criteria.setProjection(Projections.max("id"));

    Object maxId = criteria.uniqueResult();

    if (maxId != null) {
        criteria = this.createCriteria();
        criteria.add(Restrictions.eq("id", (Long) maxId));
        return ((Refueling) criteria.uniqueResult()).getOdometer();
    }
    return 0;
}

From source file:br.com.webbudget.domain.model.repository.logbook.RefuelingRepository.java

License:Open Source License

/**
 *
 * @param filter/*from  w  w  w . j a  v  a 2s.c om*/
 * @param pageRequest
 * @return
 */
@Override
public Page<Refueling> listLazily(String filter, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (StringUtils.isNotBlank(filter)) {
        // TODO colocar os filtros aqui
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.miscellany.ClosingRepository.java

License:Open Source License

/**
 *
 * @return/*  ww  w  .j av a 2  s .c  o m*/
 */
@Override
public BigDecimal findLastAccumulated() {

    final Criteria criteria = this.createCriteria();

    final DetachedCriteria mostRecent = DetachedCriteria.forClass(Closing.class)
            .setProjection(Projections.max("closingDate"));

    criteria.add(Property.forName("closingDate").eq(mostRecent));
    criteria.setProjection(Projections.sum("accumulated"));

    return (BigDecimal) criteria.uniqueResult();
}

From source file:br.com.webbudget.domain.model.repository.miscellany.FinancialPeriodRepository.java

License:Open Source License

/**
 * //  www.  j a  v  a2s .c o m
 * @param isClosed
 * @param pageRequest
 * @return 
 */
@Override
public Page<FinancialPeriod> listByStatusLazily(Boolean isClosed, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (isClosed != null) {
        criteria.add(Restrictions.eq("closed", isClosed));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}