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.tpartner.persistence.crud.implementation.GenericCRUDImplementation.java

@Override
public T findById(ID entityId) {
    Session session = getCurrentSession();
    Criteria createCriteria = session.createCriteria(persistentClass);
    createCriteria.add(Restrictions.eq("id", entityId));
    T entity = (T) createCriteria.uniqueResult();
    session.close();/*from w  ww . j a  va2 s .c om*/
    return entity;
}

From source file:br.com.vendas.dao.application.LicenseDAOImpl.java

License:Open Source License

@Override
public License findByUserId(Integer userId) {

    Session session = getSession();//from  ww  w.j  a  v a  2s .c o  m

    Criteria criteria = session.createCriteria(License.class).add(Restrictions.eq("user.userID", userId));

    return (License) criteria.uniqueResult();

}

From source file:br.com.webbudget.domain.model.repository.entries.CardInvoiceRepository.java

License:Open Source License

/**
 *
 * @param movement//  w  ww. j a  v a2 s. c  om
 * @return
 */
@Override
public CardInvoice findByMovement(Movement movement) {

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

    criteria.createAlias("movement", "mv");
    criteria.add(Restrictions.eq("mv.id", movement.getId()));

    return (CardInvoice) criteria.uniqueResult();
}

From source file:br.com.webbudget.domain.model.repository.entries.CardInvoiceRepository.java

License:Open Source License

/**
 *
 * @param card//from   w  ww.  j a v  a  2 s.c  o  m
 * @param pageRequest
 * @return
 */
@Override
public Page<CardInvoice> listByCard(Card card, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    // filtra
    if (card != null) {
        criteria.createAlias("card", "ca");
        criteria.add(Restrictions.eq("ca.id", card.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.entries.CardRepository.java

License:Open Source License

/**
 *
 * @param number//from   w  ww .  j av  a2 s  . co m
 * @param cardType
 * @return
 */
@Override
public Card findByNumberAndType(String number, CardType cardType) {

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

    criteria.add(Restrictions.eq("number", number));
    criteria.add(Restrictions.eq("cardType", cardType));

    return (Card) criteria.uniqueResult();
}

From source file:br.com.webbudget.domain.model.repository.entries.CardRepository.java

License:Open Source License

/**
 * //from w  ww  . j  a v  a2s.co m
 * @param isBlocked
 * @param pageRequest
 * @return 
 */
@Override
public Page<Card> listByStatus(Boolean isBlocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

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

    // 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.entries.ContactRepository.java

License:Open Source License

/**
 *
 * @param filter// ww w.ja va2 s.  com
 * @param blocked
 * @param pageRequest
 * @return
 */
@Override
public Page<Contact> listLazilyByFilter(String filter, Boolean blocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (filter != null) {
        criteria.add(Restrictions.or(Restrictions.ilike("name", "%" + filter + "%"),
                Restrictions.ilike("email", "%" + filter + "%"), Restrictions.eq("document", filter),
                Restrictions.ilike("city", "%" + filter + "%")));
    }

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

    // 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.entries.CostCenterRepository.java

License:Open Source License

/**
 *
 * @param name/*  w w w  .  j av a 2s  .c o m*/
 * @param parent
 * @return
 */
@Override
public CostCenter findByNameAndParent(String name, CostCenter parent) {

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

    if (parent != null) {
        criteria.createAlias("parentCostCenter", "pcc");
        criteria.add(Restrictions.eq("pcc.name", parent.getName()));
    } else {
        criteria.add(Restrictions.isNull("parentCostCenter"));
    }

    criteria.add(Restrictions.eq("name", name));

    return (CostCenter) criteria.uniqueResult();
}

From source file:br.com.webbudget.domain.model.repository.entries.CostCenterRepository.java

License:Open Source License

/**
 * /*from  w w w.j  a v  a 2s  .c o  m*/
 * @param isBlocked
 * @param pageRequest
 * @return 
 */
@Override
public Page<CostCenter> listLazilyByStatus(Boolean isBlocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

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

    // 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.entries.MovementClassRepository.java

License:Open Source License

/**
 * /* w  w w  .  ja  va 2s . c  o m*/
 * @param isBlocked
 * @param pageRequest
 * @return 
 */
@Override
public Page<MovementClass> listLazilyByStatus(Boolean isBlocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

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

    // 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);
}