Example usage for org.hibernate Criteria createAlias

List of usage examples for org.hibernate Criteria createAlias

Introduction

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

Prototype

public Criteria createAlias(String associationPath, String alias) throws HibernateException;

Source Link

Document

Join an association, assigning an alias to the joined association.

Usage

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

License:Open Source License

/**
 *
 * @param vehicle/*from  w w  w .  ja  va 2  s . c  om*/
 * @return
 */
@Override
public List<Entry> listByVehicle(Vehicle vehicle) {

    final Criteria criteria = this.createCriteria();

    criteria.createAlias("vehicle", "v");
    criteria.add(Restrictions.eq("v.id", vehicle.getId()));

    return criteria.list();
}

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

License:Open Source License

/**
 *
 * @param vehicle//from   ww  w .ja  v  a  2  s  . com
 * @param filter
 * @return
 */
@Override
public List<Entry> listByVehicleAndFilter(Vehicle vehicle, String filter) {

    final Criteria criteria = this.createCriteria();

    criteria.createAlias("vehicle", "v");
    criteria.add(Restrictions.eq("v.id", vehicle.getId()));

    if (StringUtils.isNoneBlank(filter)) {
        criteria.createAlias("movementClass", "mc");
        criteria.add(Restrictions.or(Restrictions.ilike("place", filter + "%"),
                Restrictions.ilike("title", filter + "%"), Restrictions.ilike("mc.name", filter + "%"),
                Restrictions.ilike("description", "%" + filter + "%")));
    }

    return criteria.list();
}

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

License:Open Source License

/**
 * //  w  w  w .j a  v  a2  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

/**
 *
 * @return//from  w w  w  . j  a  v  a2  s  . 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

/**
 * /*from www .  jav  a2s .  c om*/
 * @param vehicle
 * @return 
 */
@Override
public List<Refueling> findUnaccountedsForVehicle(Vehicle vehicle) {

    final Criteria criteria = this.createCriteria();

    criteria.createAlias("vehicle", "ve");
    criteria.add(Restrictions.eq("ve.id", vehicle.getId()));
    criteria.add(Restrictions.eq("accounted", false));

    return criteria.list();
}

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

License:Open Source License

/**
 * /*from w w  w.j  av a  2 s.c  o  m*/
 * @param message
 * @return 
 */
@Override
public List<UserMessage> listByMessage(Message message) {

    final Criteria criteria = this.createCriteria();

    criteria.createAlias("message", "ms");
    criteria.add(Restrictions.eq("ms.id", message.getId()));

    return criteria.list();
}

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

License:Open Source License

/**
 * //  w  w  w .j a  va2s.com
 * @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 cardInvoice/*from   w ww  .java 2s .c  o m*/
 * @return
 */
@Override
public List<Movement> listByCardInvoice(CardInvoice cardInvoice) {

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

    criteria.createAlias("cardInvoice", "ci");
    criteria.add(Restrictions.eq("ci.id", cardInvoice.getId()));

    return criteria.list();
}

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

License:Open Source License

/**
 *
 * @param filter/*  w  w w .  j  a  v a2  s.co 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.movement.MovementRepository.java

License:Open Source License

/**
 *
 * @param financialPeriod/*from  w  ww.ja  va 2s  .c o m*/
 * @param card
 * @return
 */
@Override
public List<Movement> listPaidWithoutInvoiceByPeriodAndCard(FinancialPeriod financialPeriod, Card card) {

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

    criteria.add(Restrictions.isNull("cardInvoice"));
    criteria.add(Restrictions.eq("cardInvoicePaid", Boolean.FALSE));
    criteria.add(Restrictions.eq("movementStateType", MovementStateType.PAID));

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

    criteria.createAlias("payment", "py").createAlias("py.card", "cc");
    criteria.add(Restrictions.eq("cc.id", card.getId()));

    return criteria.list();
}