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.miscellany.ClosingRepository.java

License:Open Source License

/**
 *
 * @return/* w  ww.  j  a va 2  s.  c om*/
 */
@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

/**
 *
 * @param identification//from   w w w . j  av  a2  s . co m
 * @return
 */
@Override
public FinancialPeriod findByIdentification(String identification) {

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

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

    return (FinancialPeriod) criteria.uniqueResult();
}

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

License:Open Source License

/**
 * //from w  w w  . ja  va  2  s  .c om
 * @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.miscellany.FinancialPeriodRepository.java

License:Open Source License

/**
 * /* w  w  w  .  j a va 2s.com*/
 * @return 
 */
@Override
public FinancialPeriod findLatestClosed() {

    final Criteria criteria = this.createCriteria();

    DetachedCriteria maxId = DetachedCriteria.forClass(FinancialPeriod.class)
            .add(Restrictions.eq("closed", true)).setProjection(Projections.max("id"));

    criteria.add(Property.forName("id").eq(maxId));

    return (FinancialPeriod) criteria.uniqueResult();
}

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

License:Open Source License

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

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

    return (Configuration) criteria.uniqueResult();
}

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

License:Open Source License

/**
 * //  w  ww  .  ja v  a  2  s. co  m
 * @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  w  ww . j a  v  a2s  . c om
 * @param recipient
 * @return 
 */
@Override
public long countUnread(User recipient) {

    final Criteria criteria = this.createCriteria();

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

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

License:Open Source License

/**
 * // w ww.j a  v a  2  s.  c  o m
 * @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.user.UserRepository.java

License:Open Source License

/**
 * /*from w  ww .j  a  v  a2 s  .c o  m*/
 * @param login
 * @return 
 */
@Override
public User findByUsername(String login) {

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

    criteria.add(Restrictions.eq("username", login));

    return (User) criteria.uniqueResult();
}

From source file:br.controller.ClienteDAO.java

public Cliente validar(String email, String password) {
    session = HibernateUtil.getSessionFactory().openSession();
    Cliente cli = null;/*from  www .  ja v a2 s  .c o  m*/
    try {
        Cliente test = null;
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(Cliente.class);
        c.add(Restrictions.like("email", email));
        test = (Cliente) c.uniqueResult();
        System.out.println(test.getEmail());
        if (test.getSenha().equals(password))
            cli = test;
        session.getTransaction().commit();
    } catch (NullPointerException e) {
        return null;
    } finally {
        session.close();
    }
    if (cli == null)
        return null;
    return cli;
}