Example usage for org.hibernate Criteria uniqueResult

List of usage examples for org.hibernate Criteria uniqueResult

Introduction

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

Prototype

public Object uniqueResult() throws HibernateException;

Source Link

Document

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Usage

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

License:Open Source License

/**
 * /*from www  . j  a  v a 2s  .  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

/**
 * // w ww . j  a  v a  2s.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

/**
 * /* ww  w . java 2s . c om*/
 * @param movementCode
 * @return 
 */
@Override
public Movement findByCode(String movementCode) {

    final Criteria criteria = this.createCriteria();

    criteria.add(Restrictions.eq("code", movementCode));

    return (Movement) criteria.uniqueResult();
}

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

License:Open Source License

/**
 *
 * @param filter// w w w . ja va  2 s  .  c  o 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 av  a 2  s  .co  m
 * @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.EntryRepository.java

License:Open Source License

/**
 *
 * @param code//from ww w .j a va 2 s  . c o m
 * @return
 */
@Override
public Entry findByMovementCode(String code) {

    final Criteria criteria = this.createCriteria();

    criteria.add(Restrictions.eq("financial", true));
    criteria.add(Restrictions.eq("movementCode", code));

    return (Entry) criteria.uniqueResult();
}

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

License:Open Source License

/**
 * //from  www  .  ja  va 2 s.  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

/**
 * // w  w  w  . j a  v  a 2 s .c  o m
 * @param code
 * @return 
 */
@Override
public Refueling findByMovementCode(String code) {

    final Criteria criteria = this.createCriteria();

    criteria.add(Restrictions.eq("movementCode", code));

    return (Refueling) criteria.uniqueResult();
}

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

License:Open Source License

/**
 *
 * @return//from 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 av  a2 s . co  m
 * @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);
}