Example usage for org.hibernate Query uniqueResult

List of usage examples for org.hibernate Query uniqueResult

Introduction

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

Prototype

R uniqueResult();

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.suricattus.surispring.framework.service.GenericRetrieveService.java

License:Open Source License

/**
 * Retorna a entidade de acordo com a namedQuery informada.
 * @param <T>//from   w w  w  .ja v a2  s  . c o  m
 * @param classe
 * @param namedQuery
 * @param params
 * @return
 */
@SuppressWarnings("unchecked")
public <T> T retrieveUniqueByNamedQuery(Class<T> classe, String namedQuery, Object... params) {
    Query query = getSession().getNamedQuery(namedQuery);
    if (params != null) {
        for (int i = 0; i < params.length; i++) {
            query.setParameter(i, params[i]);
        }
    }
    return (T) query.uniqueResult();
}

From source file:br.com.suricattus.surispring.framework.service.GenericRetrieveService.java

License:Open Source License

/**
 * Retorna a entidade de acordo com a namedQuery informada.
 * @param <T>// ww  w .  ja va 2 s.  co  m
 * @param classe
 * @param namedQuery
 * @param params
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> T retrieveUniqueByNamedQuery(Class<T> classe, String namedQuery, Map<String, Object> params) {
    Query query = getSession().getNamedQuery(namedQuery);
    if (params != null) {
        for (String paramName : params.keySet()) {
            Object param = params.get(paramName);
            if (param instanceof Collection) {
                query.setParameterList(paramName, (Collection) params.get(paramName));
            } else if (param instanceof Object[]) {
                query.setParameterList(paramName, (Object[]) params.get(paramName));
            } else {
                query.setParameter(paramName, params.get(paramName));
            }
        }
    }
    return (T) query.uniqueResult();
}

From source file:br.com.suricattus.surispring.framework.service.GenericRetrieveService.java

License:Open Source License

/**
 * Retorna o resultado unico de acordo com a namedQuery informada.
 * @param classe//from w w w . j a  va 2s .  c om
 * @param namedQuery
 * @param params
 * @return
 */
public Object retrieveUniqueByNamedQuery(String namedQuery, Object... params) {
    Query query = getSession().getNamedQuery(namedQuery);
    if (params != null)
        for (int i = 0; i < params.length; i++)
            query.setParameter(i, params[i]);
    return query.uniqueResult();
}

From source file:br.com.suricattus.surispring.framework.service.GenericRetrieveService.java

License:Open Source License

/**
 * Retorna o resultado unico de acordo com a namedQuery informada.
 * @param classe//from  w w w  .jav a2s  . co  m
 * @param namedQuery
 * @param params
 * @return
 */
@SuppressWarnings("rawtypes")
public Object retrieveUniqueByNamedQuery(String namedQuery, Map<String, Object> params) {
    Query query = getSession().getNamedQuery(namedQuery);
    if (params != null) {
        for (String paramName : params.keySet()) {
            Object param = params.get(paramName);
            if (param instanceof Collection) {
                query.setParameterList(paramName, (Collection) params.get(paramName));
            } else if (param instanceof Object[]) {
                query.setParameterList(paramName, (Object[]) params.get(paramName));
            } else {
                query.setParameter(paramName, params.get(paramName));
            }
        }
    }
    return query.uniqueResult();
}

From source file:br.com.thiaguten.persistence.spi.provider.hibernate.HibernatePersistenceProvider.java

License:Apache License

/**
 * {@inheritDoc}//  w ww.  j a  va 2 s.co m
 */
@Override
public <T extends Number> T countByNamedQueryAndNamedParams(Class<T> resultClazz, String queryName,
        Map<String, ?> params) {
    Query hibernateQuery = getSession().getNamedQuery(queryName);
    if (params != null && !params.isEmpty()) {
        for (final Map.Entry<String, ?> param : params.entrySet()) {
            hibernateQuery.setParameter(param.getKey(), param.getValue());
        }
    }
    return (T) hibernateQuery.uniqueResult();
}

From source file:br.com.thiaguten.persistence.spi.provider.hibernate.HibernatePersistenceProvider.java

License:Apache License

/**
 * {@inheritDoc}/*  w w  w. j a  v a2  s .  c  o  m*/
 */
@Override
public <T extends Number> T countByQueryAndNamedParams(Class<T> resultClazz, String query,
        Map<String, ?> params) {
    Query hibernateQuery = getSession().createQuery(query);
    if (params != null && !params.isEmpty()) {
        for (final Map.Entry<String, ?> param : params.entrySet()) {
            hibernateQuery.setParameter(param.getKey(), param.getValue());
        }
    }
    return (T) hibernateQuery.uniqueResult();
}

From source file:br.edu.ifes.sr.dw.persistencia.ClienteDaoHibernate.java

@Override
public Cliente buscar(String cpf) {
    session.getTransaction().begin();/*  www . j  av  a 2  s  . com*/
    String hql = "select c from Cliente c where c.cpf = :cpf";
    Query consulta = session.createQuery(hql);
    consulta.setString("cpf", cpf);
    Cliente cliente = (Cliente) consulta.uniqueResult();
    session.getTransaction().commit();
    session.close();
    return cliente;
}

From source file:br.edu.ifes.sr.dw.persistencia.ClienteDaoHibernate.java

@Override
public Cliente buscarPorEmail(String email) {
    session.getTransaction().begin();/*  ww  w .  j a va2 s .com*/
    String hql = "select c from Cliente c where c.email = :email";
    Query consulta = session.createQuery(hql);
    consulta.setString("email", email);
    Cliente cliente = (Cliente) consulta.uniqueResult();
    session.getTransaction().commit();
    session.close();
    return cliente;
}

From source file:br.edu.ifes.sr.dw.persistencia.ClienteDaoHibernate.java

@Override
public Cliente validarLogin(String email, String senha) {
    session.getTransaction().begin();/*  w w w.  jav  a  2s .  co m*/
    String hql = "select c from Cliente c where c.email = :email and c.senha = :senha ";
    Query consulta = session.createQuery(hql);
    consulta.setString("email", email);
    consulta.setString("senha", senha);
    Cliente cliente = (Cliente) consulta.uniqueResult();
    session.getTransaction().commit();
    session.close();
    return cliente;
}

From source file:br.edu.ifes.sr.dw.persistencia.CupomDaoHibernate.java

@Override
public Cupom buscarCodigoCpfCliente(String codigo, String cpf) {
    session.getTransaction().begin();/*from  w  ww.j a  va 2  s  .c o m*/
    String hql = "select c from Cupom c inner join c.cliente cl where c.codigo = :codigo and cl.cpf = :cpf";
    Query consulta = session.createQuery(hql);
    consulta.setString("codigo", codigo);
    consulta.setString("cpf", cpf);
    Cupom cupom = (Cupom) consulta.uniqueResult();
    session.getTransaction().commit();
    session.close();
    return cupom;
}