Example usage for org.hibernate Criteria setResultTransformer

List of usage examples for org.hibernate Criteria setResultTransformer

Introduction

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

Prototype

public Criteria setResultTransformer(ResultTransformer resultTransformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

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
 * @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/*  w ww  .  j  av a 2 s  .c  om*/
 * @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.logbook.RefuelingRepository.java

License:Open Source License

/**
 *
 * @param filter/*  w  ww. j  ava2 s.  com*/
 * @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.FinancialPeriodRepository.java

License:Open Source License

/**
 * /*from w ww  .  ja v a  2s. 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);
}

From source file:br.com.webbudget.domain.model.repository.tools.MessageRepository.java

License:Open Source License

/**
 * /*from  w  w  w  . java  2s. com*/
 * @param sender
 * @param filter
 * @param pageRequest
 * @return 
 */
@Override
public Page<Message> listSent(User sender, String filter, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (filter != null) {
        criteria.add(Restrictions.or(Restrictions.ilike("title", "%" + filter + "%"),
                Restrictions.ilike("content", "%" + filter + "%")));
    }

    criteria.add(Restrictions.eq("deleted", false));
    criteria.add(Restrictions.eq("sender", sender));

    // 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.tools.UserMessageRepository.java

License:Open Source License

/**
 * /*from  ww  w .j  av a  2 s  .c om*/
 * @param recipient
 * @param filter
 * @param pageRequest
 * @return 
 */
@Override
public Page<UserMessage> listReceived(User recipient, String filter, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (filter != null) {
        criteria.createAlias("message", "ms");
        criteria.add(Restrictions.or(Restrictions.ilike("ms.title", "%" + filter + "%"),
                Restrictions.ilike("ms.content", "%" + filter + "%")));
    }

    criteria.add(Restrictions.eq("deleted", false));
    criteria.add(Restrictions.eq("recipient", recipient));

    // 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.repository.movement.MovementRepository.java

License:Open Source License

/**
 *
 * @param filter/*  w  w  w . ja  v  a  2 s . c  o  m*/
 * @param paid
 * @return
 */
@Override
public List<Movement> listByFilter(String filter, Boolean paid) {

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

    if (paid != null && paid.equals(Boolean.TRUE)) {
        criteria.add(Restrictions.isNotNull("payment"));
    }

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

    // se conseguir castar para bigdecimal trata como um filtro
    try {
        final BigDecimal value = new BigDecimal(filter);
        criteria.add(Restrictions.or(Restrictions.eq("code", filter), Restrictions.eq("value", value),
                Restrictions.ilike("description", filter + "%"), Restrictions.ilike("mc.name", filter + "%"),
                Restrictions.ilike("cc.name", filter + "%"),
                Restrictions.ilike("fp.identification", filter + "%")));
    } catch (NumberFormatException ex) {
        criteria.add(Restrictions.or(Restrictions.eq("code", filter),
                Restrictions.ilike("description", filter + "%"), Restrictions.ilike("mc.name", filter + "%"),
                Restrictions.ilike("cc.name", filter + "%"),
                Restrictions.ilike("fp.identification", filter + "%")));
    }

    criteria.addOrder(Order.desc("inclusion"));

    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    return criteria.list();
}

From source file:br.com.webbudget.domain.repository.user.UserPrivateMessageRepository.java

License:Open Source License

/**
 * /*from www.j a v a 2 s.c o m*/
 * @param user
 * @param showUnread
 * @return 
 */
@Override
public List<UserPrivateMessage> listByUser(User user, Boolean showUnread) {

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

    criteria.createAlias("recipient", "u");
    criteria.add(Restrictions.eq("u.id", user.getId()));

    if (showUnread != null) {
        criteria.add(Restrictions.eq("wasRead", false));
    }

    // nao mostra mensagens deletadas nunca
    criteria.add(Restrictions.eq("deleted", false));

    criteria.addOrder(Order.desc("inclusion"));

    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    return criteria.list();
}

From source file:br.com.webbudget.domain.repository.user.UserPrivateMessageRepository.java

License:Open Source License

@Override
public List<UserPrivateMessage> listReceipts(PrivateMessage privateMessage) {

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

    criteria.createAlias("privateMessage", "pm");
    criteria.add(Restrictions.eq("pm.id", privateMessage.getId()));

    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    return criteria.list();
}

From source file:br.gov.jfrj.siga.hibernate.ExDao.java

License:Open Source License

public void indexarUltimas(int desde) throws Exception {

    System.out.println("Indexando documentos...");
    long inicio = new Date().getTime();

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, desde);
    Date dtIni = cal.getTime();//from  w  w w.  j a  v a 2 s . c o  m

    Criteria crit = getSessao().createCriteria(ExDocumento.class);
    crit.createCriteria("exMovimentacaoSet").add(Restrictions.gt("dtIniMov", dtIni));
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    /*
     * Query indexQuery = getSessao() .createQuery( "from ExDocumento as doc
     * inner join doc.exMovimentacaoSet as mov where mov.dtIniMov >=
     * :dtIni"); indexQuery.setParameter("dtIni", dtIni);
     */

    FullTextSession fullTextSession = Search.getFullTextSession(getSessao());
    // List<ExDocumento> list = indexQuery.list();
    List<ExDocumento> list = crit.list();
    Transaction tx = fullTextSession.beginTransaction();
    for (ExDocumento doc : list) {
        System.out.println(" . " + doc.getIdDoc());
        fullTextSession.purge(ExDocumento.class, doc);
        if (doc.isIndexavel())
            fullTextSession.index(doc);
    }
    tx.commit();
    fullTextSession.clear();
    getSessao().clear();
    //System.gc();

    // fullTextSession.close();
    System.out.println("Durao da indexao de documentos: " + (new Date().getTime() - inicio));

}