Example usage for org.hibernate SQLQuery uniqueResult

List of usage examples for org.hibernate SQLQuery uniqueResult

Introduction

In this page you can find the example usage for org.hibernate SQLQuery 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:org.snaker.engine.access.hibernate.HibernateAccess.java

License:Apache License

@SuppressWarnings("unchecked")
public <T> T queryObject(Class<T> T, String sql, Object... args) {
    SQLQuery query = getSession().createSQLQuery(sql);
    query.addEntity(T);//  w  w w  .  j ava2 s.  c  om
    for (int i = 0; i < args.length; i++) {
        query.setParameter(i, args[i]);
    }
    return (T) query.uniqueResult();
}

From source file:org.snaker.engine.access.hibernate.HibernateAccess.java

License:Apache License

public Object queryCount(String sql, Object... args) {
    SQLQuery countQuery = getSession().createSQLQuery(sql);
    if (args.length > 0) {
        for (int i = 0; i < args.length; i++) {
            countQuery.setParameter(i, args[i]);
        }/*  w  ww  .  j  a  v a 2  s  . c om*/
    }
    return countQuery.uniqueResult();
}

From source file:org.snaker.engine.access.hibernate3.HibernateAccess.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from   w ww  . j  av a  2  s  .com
public <T> T queryObject(Class<T> T, String sql, Object... args) {
    SQLQuery query = getSession().createSQLQuery(sql);
    query.addEntity(T);
    for (int i = 0; i < args.length; i++) {
        query.setParameter(i, args[i]);
    }
    return (T) query.uniqueResult();
}

From source file:org.snaker.engine.access.hibernate3.HibernateAccess.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from   www .  j  a  va 2 s .co m*/
public <T> List<T> queryList(Page<T> page, Class<T> T, String sql, Object... args) {
    try {
        String countSQL = "select count(1) from (" + sql + ") c ";
        String querySQL = sql;
        if (page.isOrderBySetted()) {
            querySQL = querySQL + StringHelper.buildPageOrder(page.getOrder(), page.getOrderBy());
        }
        SQLQuery countQuery = getSession().createSQLQuery(countSQL);
        SQLQuery pageQuery = getSession().createSQLQuery(querySQL);
        pageQuery.addEntity(T);
        if (args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                pageQuery.setParameter(i, args[i]);
                countQuery.setParameter(i, args[i]);
            }
        }
        //???pageSize
        if (page.getPageSize() != Page.NON_PAGE) {
            pageQuery.setFirstResult((page.getPageNo() - 1) * page.getPageSize());
            pageQuery.setMaxResults(page.getPageSize());
        }
        List<T> list = pageQuery.list();
        Object total = countQuery.uniqueResult();
        page.setResult(list);
        page.setTotalCount(ClassHelper.castLong(total));
        return list;
    } catch (RuntimeException e) {
        log.error(e.getMessage(), e);
        return Collections.emptyList();
    }
}

From source file:org.workin.persistence.hibernate.v3.dao.Hibernate3PersistenceDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//w  ww. jav a  2  s .  com
public <R> R findUniqueByNativeQuery(final Class<R> resultClass, final String sql, final Object[] values) {

    return getHibernateTemplate().execute(new HibernateCallback<R>() {

        @Override
        public R doInHibernate(Session session) throws HibernateException, SQLException {
            SQLQuery query = session.createSQLQuery(sql);
            query.addEntity(resultClass);
            setQueryParameters(query, values);
            return (R) query.uniqueResult();
        }
    });
}

From source file:org.workin.persistence.hibernate.v3.dao.Hibernate3PersistenceDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override// w  w w.j a  va 2 s  .com
public <R> R findUniqueByNativeQuery(final Class<R> resultClass, final String sql,
        final Map<String, ?> paramMap) {

    return getHibernateTemplate().execute(new HibernateCallback<R>() {

        @Override
        public R doInHibernate(Session session) throws HibernateException, SQLException {
            SQLQuery query = session.createSQLQuery(sql);
            query.addEntity(resultClass);
            setQueryNamedParameters(query, paramMap);
            return (R) query.uniqueResult();
        }
    });
}

From source file:org.workin.persistence.hibernate.v4.dao.Hibernate4PersistenceDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from   ww w.j ava2s . c  o  m*/
public <R> R findUniqueByNativeQuery(final Class<R> resultClass, final String sql, final Object[] values) {
    SQLQuery query = getCurrentSession().createSQLQuery(sql);
    query.addEntity(resultClass);
    setQueryParameters(query, values);
    return (R) query.uniqueResult();
}

From source file:org.workin.persistence.hibernate.v4.dao.Hibernate4PersistenceDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/* www  .  j  a v  a  2 s  .  c  o m*/
public <R> R findUniqueByNativeQuery(final Class<R> resultClass, final String sql,
        final Map<String, ?> paramMap) {
    SQLQuery query = getCurrentSession().createSQLQuery(sql);
    query.addEntity(resultClass);
    setQueryNamedParameters(query, paramMap);
    return (R) query.uniqueResult();
}

From source file:oscar.appt.status.dao.hibernate.AppointmentStatusDAOHibernate.java

License:Open Source License

public int checkStatusUsuage(List allStatus) {
    int iUsuage = 0;
    SQLQuery query = null;
    AppointmentStatus apptStatus = null;
    String sql = null;//from   w  w w  . ja  va  2s.  c o  m
    for (int i = 0; i < allStatus.size(); i++) {
        apptStatus = (AppointmentStatus) allStatus.get(i);
        if (apptStatus.getActive() == 1)
            continue;
        sql = "select count(*) as total from appointment where status like" + "'" + apptStatus.getStatus()
                + "%' ";
        sql = sql + "collate latin1_general_cs";
        query = getSession().createSQLQuery(sql);
        query.addScalar("total", Hibernate.INTEGER);
        iUsuage = (Integer) query.uniqueResult();
        if (iUsuage > 0) {
            iUsuage = i;
            break;
        }
    }
    return iUsuage;
}

From source file:pe.gob.onpe.rae.dao.hibernate.ExpedientePadronDAOH.java

@Override
public Integer getCountExportarExpediente(int id) {
    String sql = "SELECT COUNT(N_ORDEN_REGISTRO) FROM TAB_EXPEDIENTE_PADRON WHERE N_EXPEDIENTE = :lista ORDER BY N_ORDEN_REGISTRO";
    SQLQuery query = this.getSession().createSQLQuery(sql);
    query.setParameter("lista", id);
    return (Integer) ((BigDecimal) query.uniqueResult()).intValueExact();

}