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:at.ac.tuwien.ifs.tita.dao.time.EffortDao.java

License:Apache License

/** {@inheritDoc} */
@Override/*from   w  ww.j  a v a  2  s.c  om*/
public Long findEffortsSumForIssueTrackerTask(Long tpId, String username, Long issTProjectId, Long isstTTaskId,
        Long isstId) {
    String queryString = "select sum(duration) as duration from effort e "
            + "join issue_tracker_task itt on e.issuet_task_id = itt.id "
            + "join issue_tracker_project itp on itt.issue_tracker_project_id = itp.id "
            + "join tita_project tp on itp.tita_project_id = tp.id " + "join tita_user tu on e.user_id = tu.id "
            + "where tp.id = ? and itp.isst_id = ? and itp.isst_project_id = ? "
            + "and itt.isst_task_id = ? and tu.username = ?";

    org.hibernate.SQLQuery q = getSession().createSQLQuery(queryString.toUpperCase());
    // q.addEntity(Long.class);
    // CHECKSTYLE:OFF
    q.setParameter(0, tpId);
    q.setParameter(1, isstId);
    q.setParameter(2, issTProjectId);
    q.setParameter(3, isstTTaskId);
    q.setParameter(4, username);
    // CHECKSTYLE:ON

    Object obj = q.uniqueResult();

    if (obj != null) {
        return new Long(((BigDecimal) obj).longValue());
    }
    return null;
}

From source file:baza.Broker.java

public int vratiNajveceg() {
    int br;/*from  www. java  2s . co  m*/
    Session session = Test.getSessionFactory().openSession();
    session.beginTransaction();
    String sql = "SELECT MAX(BrojPregleda) FROM pregled";

    SQLQuery query = session.createSQLQuery(sql);
    br = (Integer) query.uniqueResult();

    session.close();
    return br;

}

From source file:br.com.bluesoft.pronto.dao.SprintDao.java

License:Open Source License

private void preencheTotaisDeEsforcoEValorDeNegocioDoSprint(final Sprint sprint) {
    final String sql = "select sprint, sum(t.valor_de_negocio) as valor_de_negocio_total, sum(t.esforco) as esforco_total from ticket t where t.sprint = :sprint and t.pai is null group by sprint";
    final SQLQuery query = getSession().createSQLQuery(sql);
    query.setInteger("sprint", sprint.getSprintKey());
    query.addScalar("sprint", Hibernate.INTEGER);
    query.addScalar("valor_de_negocio_total", Hibernate.INTEGER);
    query.addScalar("esforco_total", Hibernate.DOUBLE);
    final Object[] o = (Object[]) query.uniqueResult();

    int valorDeNegocioTotal = 0;
    double esforcoTotal = 0d;

    if (o != null) {
        valorDeNegocioTotal = (Integer) o[1];
        esforcoTotal = (Double) o[2];
    }//from   w  w  w. java 2 s .c  o  m

    sprint.setEsforcoTotal(esforcoTotal);
    sprint.setValorDeNegocioTotal(valorDeNegocioTotal);

}

From source file:br.com.chamados.control.DAO.java

public Object querySQL(String sql) {
    openSession();
    SQLQuery query = session.createSQLQuery(sql);
    return query.uniqueResult();
}

From source file:br.com.financeiro.dao.LancamentoDAOHibernate.java

@Override
public float saldo(Conta conta, Date data) {
    StringBuffer sql = new StringBuffer();

    sql.append("select sum(l.valor * c.fator)");
    sql.append("from LANCAMENTO l,");
    sql.append("  CATEGORIA c");
    sql.append(" where l.categoria = c.codigo");
    sql.append("  and l.conta = :conta");
    sql.append("  and l.data <= :data");
    SQLQuery query = this.session.createSQLQuery(sql.toString());
    query.setParameter("conta", conta.getConta());
    query.setParameter("data", data);
    BigDecimal saldo = (BigDecimal) query.uniqueResult();
    if (saldo != null) {
        return saldo.floatValue();
    }/* w w w .ja v a 2 s.  c  o m*/

    return 0f;
}

From source file:br.com.rhmanager.daoImpl.HibernateDAO.java

public Timestamp getHoraServer() {
    Session session = null;/*from   w  ww. j  a  va2  s. c  om*/
    try {
        session = HibernateUtil.getSession();
        session.beginTransaction();
        SQLQuery query = session.createSQLQuery("SELECT NOW()");
        Timestamp time = (Timestamp) query.uniqueResult();
        return time;
    } catch (HibernateException e) {
        e.printStackTrace();
        return null;
    } finally {
        session.close();
    }

}

From source file:celepsa.rrcc.da.AgrupacionDA.java

private Integer CrearIDAgrupacion() throws Exception {
    Integer idnew = 0;/*from   w w w . j a  v  a2  s.co m*/
    try {
        SQLQuery query = session.createSQLQuery("select max(id) from Tmstakeagrupacion");
        idnew = (Integer) query.uniqueResult();
        if (idnew != null) {
            if (idnew == 0) {
                idnew = 1;
            } else {
                idnew = idnew + 1;
            }
        } else {
            idnew = 1;
        }
        logger.debug("CrearIDAgrupacion: " + idnew);
        return idnew;
    } catch (NumberFormatException | HibernateException e) {
        logger.error(e.getMessage());
        throw e;
    }
}

From source file:celepsa.rrcc.da.DocumentoDA.java

private Integer CrearIDDoc() throws Exception {
    Integer idnew = 0;/*from   w  w  w .ja v a2 s.com*/
    try {
        SQLQuery query = session.createSQLQuery("select max(id) from Tmdocumento");
        idnew = (Integer) query.uniqueResult();
        logger.debug("CrearIDDocumento: " + idnew);
        if (idnew != null) {
            if (idnew == 0) {
                idnew = 1;
            } else {
                ++idnew;
            }
        } else {
            idnew = 1;
        }
        logger.debug("CrearIDDocumento: " + idnew);
        return idnew;
    } catch (NumberFormatException | HibernateException e) {
        logger.error(e.getMessage());
        e.printStackTrace();
        throw e;
    }

}

From source file:celepsa.rrcc.da.DocumentoDA.java

private Integer CrearIDAdjunto() throws Exception {
    Integer idnew = 0;//from  w ww  .  jav  a 2  s .c  om
    try {
        org.hibernate.Transaction tx = session.beginTransaction();
        SQLQuery query = session.createSQLQuery("select max(id) from Tmadjunto");
        idnew = (Integer) query.uniqueResult();
        if (idnew != null) {
            if (idnew == 0) {
                idnew = 1;
            } else {
                idnew = idnew + 1;
            }
        } else {
            idnew = 1;
        }
        logger.debug("CrearIDAdjunto: " + idnew);
        return idnew;
    } catch (NumberFormatException | HibernateException e) {
        logger.error(e.getMessage());
        throw e;
    }
}

From source file:celepsa.rrcc.da.PersonaDA.java

private Integer CrearIDPersona() throws Exception {
    Integer idnew = 0;//from   w  ww  .  j a v a2 s.co  m
    try {
        org.hibernate.Transaction tx = session.beginTransaction();
        SQLQuery query = session.createSQLQuery("select max(id) from Tmstakepersona");
        idnew = (Integer) query.uniqueResult();
        if (idnew != null) {
            if (idnew == 0) {
                idnew = 1;
            } else {
                idnew = idnew + 1;
            }
        } else {
            idnew = 1;
        }
        logger.debug("CrearIDPersona: " + idnew);
        return idnew;
    } catch (NumberFormatException | HibernateException e) {
        logger.error(e.getMessage());
        throw e;
    }
}