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:pe.gob.onpe.rae.dao.hibernate.ExpedientePadronDAOH.java

@Override
public Integer fetchCountExpedPadron(int expediente, int indicador) {
    String sql = "SELECT COUNT(C_DOCUMENTO_IDENTIDAD) FROM TAB_EXPEDIENTE_PADRON WHERE N_EXPEDIENTE = :expediente AND N_ESTADO NOT IN(:P_ELIMINADO,:P_RECHAZADO) AND N_INDICADOR = :indicador";
    SQLQuery query = this.getSession().createSQLQuery(sql);
    query.setParameter("expediente", expediente);
    //query.setParameter("P_OTRA_LISTA", Parametros.ESTADO_ELECTOR_REG_OTRA_LISTA);
    query.setParameter("P_ELIMINADO", Parametros.ESTADO_ELECTOR_ELIMINADO);
    query.setParameter("P_RECHAZADO", Parametros.ESTADO_ELECTOR_RECHAZADO);
    query.setParameter("indicador", indicador);
    return (Integer) ((BigDecimal) query.uniqueResult()).intValueExact();
}

From source file:podd.dataaccess.hibernate.HibernateFedoraPIDGenerator.java

License:Open Source License

@Override
// FIXME: TODO YF: This method executed concurrently will sometimes return stale values!
public synchronized String getNextPID(String namespace) throws DataAccessException {
    long start = System.currentTimeMillis();

    try {/*from w  w w .jav a2s.c om*/
        Session session = factory.currentSession();
        session.beginTransaction();
        try {
            SQLQuery sqlQuery = createSelectIDSQLQuery(namespace, session);
            Integer currentPid = (Integer) sqlQuery.uniqueResult();
            if (null == currentPid) {
                currentPid = 0;
                sqlQuery = createInsertSQLQuery(namespace, session);
            } else {
                currentPid++;
                sqlQuery = createUpdateIDSQLQuery(namespace, session, currentPid);
            }
            sqlQuery.executeUpdate();
            session.getTransaction().commit();
            return namespace + ":" + currentPid.toString();
        } catch (Exception e) {
            session.getTransaction().rollback();
            throw new DataAccessException(e);
        } finally {
            session.flush();
            factory.closeSession();
        }
    } finally {
        if (_INFO) {
            long end = System.currentTimeMillis();
            LOGGER.info("getNextPID timing=" + (end - start));
        }
    }
}

From source file:podd.dataaccess.hibernate.HibernateFedoraPIDGenerator.java

License:Open Source License

public String removeNamespace(String namespace) throws DataAccessException {
    Session session = factory.currentSession();
    try {/*from w  w w  .  j ava2s .  com*/
        session.beginTransaction();
        SQLQuery selectQuery = createSelectIDSQLQuery(namespace, session);
        Integer currentPid = (Integer) selectQuery.uniqueResult();
        if (null != currentPid) {
            SQLQuery deleteQuery = createDeleteSQLQuery(namespace, session);
            deleteQuery.executeUpdate();
        }
        session.getTransaction().commit();
        return null == currentPid ? null : namespace + ":" + currentPid;
    } catch (HibernateException e) {
        session.getTransaction().rollback();
        throw new DataAccessException(e);
    } finally {
        factory.closeSession();
    }
}

From source file:restaurantsearch.CuisineHelper.java

public Restaurant getRestaurantDetails(int restaurantId) {

    Restaurant restaurant = null;/*ww  w  .j  a v  a2  s.com*/

    String sql = "select * from restaurant where restaurant_id = :id";

    try {

        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(Restaurant.class);

        q.setParameter("id", restaurantId);

        restaurant = (Restaurant) q.uniqueResult();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return restaurant;
}

From source file:restaurantsearch.ReservationHelper.java

public Reservation getReservationDetailsByResId(int resId) {

    Reservation reservation = null;//from  ww  w.  j  a  va2  s . co m

    String sql = "select * from reservation " + "where reservation_id = :id";

    try {

        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(Reservation.class);

        q.setParameter("id", resId);

        reservation = (Reservation) q.uniqueResult();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return reservation;
}

From source file:restaurantsearch.RestaurantHelper.java

public Restaurant getRestaurantDetails(int restId) {

    Restaurant restaurant = null;//  w w w.  j ava2 s .  co m

    String sql = "select * from restaurant " + "where restaurant_id = :id";

    try {

        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(Restaurant.class);

        q.setParameter("id", restId);

        restaurant = (Restaurant) q.uniqueResult();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return restaurant;
}

From source file:restaurantsearch.UserHelper.java

public User selectUser(String email) {
    User current = null;//from w  w w  .  ja v  a2 s.c om

    String sql = "select * from user where user_email = :email";

    try {

        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(User.class);

        q.setParameter("email", email);

        current = (User) q.uniqueResult();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return current;
}

From source file:restaurantsearch.UserHelper.java

public User selectUserById(int userId) {
    User current = null;//from   w w  w . j a v  a2  s .  c  om

    String sql = "select * from user where user_id = :id";

    try {

        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(User.class);

        q.setParameter("id", userId);

        current = (User) q.uniqueResult();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return current;
}

From source file:restaurantsearch.UserHelper.java

public Reservation getRestaurantDetails(int userId) {

    Reservation reservation = null;/*from   w w w . j  av  a  2  s .c o m*/

    String sql = "select * from reservation " + "where user_id = :id";

    try {

        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(Reservation.class);

        q.setParameter("id", userId);

        reservation = (Reservation) q.uniqueResult();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return reservation;
}

From source file:ru.runa.wfe.commons.dao.ConstantDao.java

License:Open Source License

public Integer getDatabaseVersion() throws Exception {
    // we won't handle connection error
    org.hibernate.classic.Session session = sessionFactory.getCurrentSession();
    Connection connection = session.connection();
    DatabaseMetaData metaData = connection.getMetaData();
    log.info("Running with " + metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion());
    try {//from w w w  .j  av  a2 s  . co  m
        SQLQuery query = session.createSQLQuery("SELECT VALUE FROM WFE_CONSTANTS WHERE NAME=:name");
        query.setString("name", DATABASE_VERSION_VARIABLE_NAME);
        return TypeConversionUtil.convertTo(Integer.class, query.uniqueResult());
    } catch (Exception e) {
        log.warn("Unable to get database version", e);
        return null;
    }
}