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:com.iwancool.dsm.dao.impl.AbstractBaseGenericORMDaoImpl.java

License:Open Source License

@Override
public BigInteger countBySql(String sql) {
    SQLQuery q = this.getCurrentSession().createSQLQuery(sql);
    return (BigInteger) q.uniqueResult();
}

From source file:com.iwancool.dsm.dao.impl.AbstractBaseGenericORMDaoImpl.java

License:Open Source License

@Override
public BigInteger countBySql(String sql, Map<String, Object> params) {
    SQLQuery q = this.getCurrentSession().createSQLQuery(sql);
    if (params != null && !params.isEmpty()) {
        for (String key : params.keySet()) {
            q.setParameter(key, params.get(key));
        }/*  www. j a v  a2 s  . c  om*/
    }
    return (BigInteger) q.uniqueResult();
}

From source file:com.iwancool.dsm.dao.impl.AbstractBaseGenericORMDaoImpl.java

License:Open Source License

@Override
public Object getObject(String sql, Map<String, Object> params) {
    SQLQuery q = this.getCurrentSession().createSQLQuery(sql);
    if (params != null && !params.isEmpty()) {
        for (String key : params.keySet()) {
            q.setParameter(key, params.get(key));
        }/*from  ww  w  . j  av a 2 s .co m*/
    }
    return q.uniqueResult();
}

From source file:com.jfootball.dao.hibernate.PlayerDaoImpl.java

License:Open Source License

/**
 * /* w w w  . ja  v  a 2  s .c o m*/
 * chiamata alla funzione mysql 'getRank(?,?)'
 * 
 * 
 * */
public String getRank(Long teamId, Long playerId) {
    logger.info("Get Rank team" + teamId + " playerId " + playerId);

    Session session = hibernateTemplate.getSessionFactory().getCurrentSession();

    SQLQuery query = session.createSQLQuery("select getRank(?, ?)");
    query.setParameter(0, teamId);
    query.setParameter(1, playerId);

    String rank = query.uniqueResult().toString();

    logger.info("Rank returned");

    return rank;
}

From source file:com.kodemore.dao.KmDaoSession.java

License:Open Source License

private Integer _executeLockCommand(String sql) {
    SQLQuery q = null;
    try {/*from   w  ww.java2  s  .c  om*/
        q = _session.createSQLQuery(sql);
        Object res = q.uniqueResult();

        if (res instanceof BigInteger)
            return ((BigInteger) res).intValue();

        return (Integer) res;
    } catch (Exception ex) {
        throw Kmu.newFatal(ex, "Locking error; %s", sql);
    }
}

From source file:com.liyablieva.jaxws.dao.EmployeeDAOImpl.java

@Override
public int getDepartmentId(String department) {

    System.out.println("In getDepartment method.");
    String[] departments = department.split(";");
    int parentId = 0;
    Object id;/* w  w  w.j av a2s  .  c o  m*/

    for (int i = 0; i < departments.length; i++) {
        String sql = "SELECT DEPARTMENT_ID FROM DEPARTMENT WHERE department_name = :department_name AND parent_id = :parent_id";
        SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql);
        query.setParameter("department_name", departments[i]);
        query.setParameter("parent_id", parentId);
        id = query.uniqueResult();
        //uniqueResult returns null if object was not found
        if (id == null) {
            throw new IllegalArgumentException("Department was not found!");
        }
        //cast if id != null
        parentId = (int) id;
    }

    System.out.println("Department id = " + parentId);

    return parentId;
}

From source file:com.mx.teknei.pcabordo.lib.dao.impl.SbctAlarDAO.java

@Override
public SbctAlar getAlarForName(String nameAlar) {

    SbctAlar s_alar = null;//from www. java 2  s  .  c o m
    Transaction trans = null;
    Session session = null;
    try {
        session = LoadConnection.getSessionFactory().openSession();
    } catch (ExceptionInInitializerError eiie) {
        System.out.println("Error al iniciar la coneccion a BD postgres:" + eiie.getMessage());
    } catch (Exception e) {
        System.err.println("Error en LoadConnection." + e.getMessage());
    }
    try {
        trans = session.beginTransaction();
        SQLQuery query = session.createSQLQuery("select * from sitm.sbct_alar s where s.des_alar=?");
        query.addEntity(SbctAlar.class);
        query.setString(0, nameAlar);
        s_alar = (SbctAlar) query.uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.flush();
        session.close();
    }
    return s_alar;

    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

From source file:com.mx.teknei.pcabordo.lib.dao.impl.SfalConfDAO.java

@Override
public SfalConf getAlarConfForName(String nameConfAlar) {
    SfalConf s_alar = null;/* ww w.j  a  va2s  . c o  m*/
    Transaction trans = null;
    Session session = null;
    try {
        session = LoadConnection.getSessionFactory().openSession();
    } catch (ExceptionInInitializerError eiie) {
        System.out.println("Error al iniciar la coneccion a BD postgres:" + eiie.getMessage());
    } catch (Exception e) {
        System.err.println("Error en LoadConnection." + e.getMessage());
    }
    try {
        trans = session.beginTransaction();
        SQLQuery query = session.createSQLQuery(
                "SELECT * FROM sitm.sfal_conf s WHERE s.id_alar = (SELECT id_alar from sitm.sbct_alar where des_alar = ? )");
        query.addEntity(SfalConf.class);
        query.setString(0, nameConfAlar);
        s_alar = (SfalConf) query.uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.flush();
        session.close();
    }
    return s_alar;
}

From source file:com.nextep.datadesigner.vcs.services.VersionHelper.java

License:Open Source License

/**
 * Executes the specified revision-check query for the given element id. The return boolean
 * indicates whether the revision number fetched from the database through the given query
 * matches the expected revision number.
 * //from w  w w .  j a  va 2s  .  c o  m
 * @param query SQL query which can retrieve the revision number from its ID
 * @param id unique ID of element to check the revision
 * @param expectedRevision expected revision number
 * @return <code>true</code> if the expected revision number matches the repository revision
 *         number, else <code>false</code>
 */
private static boolean queryIsUpToDate(String query, long id, long expectedRevision) {
    final Session session = HibernateUtil.getInstance().getSandBoxSession();
    session.flush();
    session.clear();
    SQLQuery sqlQuery = session.createSQLQuery(query);
    sqlQuery.setLong(0, id);
    Number revision = (Number) sqlQuery.uniqueResult();
    return revision == null || (revision != null && revision.longValue() == expectedRevision);
}

From source file:com.poka.dao.impl.BaseDao.java

@Override
public BigInteger countBySql(String sql) {
    SQLQuery q = getCurrentSession().createSQLQuery(sql);
    String result = q.uniqueResult().toString();
    BigInteger count = new BigInteger(result);
    return count;
}