Example usage for org.hibernate Query setCacheable

List of usage examples for org.hibernate Query setCacheable

Introduction

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

Prototype

Query<R> setCacheable(boolean cacheable);

Source Link

Document

Enable/disable second level query (result) caching for this query.

Usage

From source file:br.mdarte.exemplo.academico.cd.EstudanteDAO.java

/**
 * Lista todos os objetos./*www . j  a  v  a  2 s .c  o m*/
 * 
 * @return Lista de objetos
 */
public List<AbstractEntity> list(PaginationStrategy paginacao, String propriedade, Boolean desc)
        throws DAOException {

    try {
        Session session = currentSession();

        String hql = "from br.mdarte.exemplo.academico.cd.Estudante";

        if (propriedade != null && !propriedade.equals("")) {
            String order = desc.booleanValue() ? " desc" : " asc";
            hql += " order by " + propriedade + order;
        }

        org.hibernate.Query qry = session.createQuery(hql);

        if (paginacao == null)
            paginacao = new NoPagination();

        paginacao.paginateResult(qry);
        qry.setCacheable(true);
        return qry.list();

    } catch (HibernateException h) {
        throw new DAOException(h);
    }
}

From source file:br.mdarte.exemplo.academico.cd.EstudanteDAO.java

public List<AbstractEntity> list() throws DAOException {
    try {/*from   ww  w  .  j  a  v  a  2s.  com*/
        Session session = currentSession();
        org.hibernate.Query qry = session.createQuery("from br.mdarte.exemplo.academico.cd.Estudante");
        qry.setCacheable(true);
        List res = qry.list();
        return res;

    } catch (HibernateException h) {
        throw new DAOException(h);
    }
}

From source file:br.mdarte.exemplo.academico.cd.EstudanteDAO.java

/**
 * //from ww w. j  a  v a2 s  . c  o  m
 */

public static br.mdarte.exemplo.academico.cd.Curso getCursoByEstudante(long id) throws DAOException {

    try {
        Session session = currentSession();
        org.hibernate.Query qry = session.createQuery(
                "select a.curso from br.mdarte.exemplo.academico.cd.Estudante a where a.id = " + id);
        qry.setCacheable(true);
        List res = qry.list();
        if (res.isEmpty()) {
            return null;
        } else {
            return (br.mdarte.exemplo.academico.cd.Curso) res.get(0);
        }
    } catch (HibernateException h) {
        throw new DAOException(h);
    }
}

From source file:ch.algotrader.dao.AbstractDao.java

License:Open Source License

protected Query prepareQuery(final LockOptions lockOptions, final String queryString, final boolean cacheable,
        final int maxResults, final QueryType type) {

    Query query = createQuery(queryString, type);
    if (lockOptions != null) {
        query.setLockOptions(lockOptions);
    }/*from   w  ww  . jav  a2 s  .  co  m*/
    query.setCacheable(cacheable);
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }
    return query;
}

From source file:ch.algotrader.dao.AbstractDao.java

License:Open Source License

protected Query prepareQuery(final LockOptions lockOptions, final String queryString, final boolean cacheable,
        final int maxResults, final QueryType type, final Object... params) {

    Query query = createQuery(queryString, type);
    if (lockOptions != null) {
        query.setLockOptions(lockOptions);
    }// w  ww  . ja v  a 2s  .co  m
    query.setCacheable(cacheable);
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }
    applyParameters(query, params);
    return query;
}

From source file:ch.algotrader.dao.AbstractDao.java

License:Open Source License

protected Query prepareQuery(final LockOptions lockOptions, final String queryString, final boolean cacheable,
        final int maxResults, final QueryType type, final NamedParam... params) {

    Query query = createQuery(queryString, type);
    if (lockOptions != null) {
        query.setLockOptions(lockOptions);
    }/* w  w  w.ja va  2 s . c  o m*/
    query.setCacheable(cacheable);
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }
    applyParameters(query, params);
    return query;
}

From source file:co.com.carpco.altablero.hibernate.dao.ClassRoomDao.java

public BzClassRoom getClassRoom(Integer index) {
    Chronometer chrono = new Chronometer();
    BzClassRoom bzClassRoom = null;/*  w ww. j av a 2  s . c o m*/
    chrono.start();

    try {
        Session session = this.getSession();
        session.beginTransaction();

        StringBuilder queryBuilder = new StringBuilder();
        queryBuilder.append("from BzClassRoom as classroom ");
        queryBuilder.append("where classroom.id = :index");

        Query query = session.createQuery(queryBuilder.toString());
        query.setParameter("index", index);
        query.setCacheable(true);
        bzClassRoom = (BzClassRoom) query.list().get(0);
    } catch (HibernateException ex) {
        EXCEPTION_LOGGER.error(ex.getMessage());
    } finally {
        chrono.stop();
        DAO_LOGGER.info("Class: ClassRoomDao, Method: getClassRoom, Executed in: " + chrono.getTime());
    }

    return bzClassRoom;
}

From source file:com.algoTrader.entity.PositionDaoBase.java

/**
 * {@inheritDoc}/*from   ww w  .ja  v  a 2s.  c  o  m*/
 */
@Override
@SuppressWarnings("unchecked")
public List<?> findOpenPositions(final int transform, final String queryString, int pageNumber, int pageSize) {
    try {
        Query queryObject = super.getSession(false).createQuery(queryString);
        queryObject.setCacheable(true);
        if (pageNumber > 0 && pageSize > 0) {
            queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            queryObject.setMaxResults(pageSize);
        }
        List results = queryObject.list();
        transformEntities(transform, results);
        return results;
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:com.algoTrader.entity.PositionDaoBase.java

/**
 * {@inheritDoc}/*from w w  w.j  a v  a2 s .  c o m*/
 */
@Override
@SuppressWarnings("unchecked")
public List<?> findOpenPositionsByStrategy(final int transform, final String queryString, int pageNumber,
        int pageSize, final String strategyName) {
    try {
        Query queryObject = super.getSession(false).createQuery(queryString);
        queryObject.setCacheable(true);
        queryObject.setParameter("strategyName", strategyName);
        if (pageNumber > 0 && pageSize > 0) {
            queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            queryObject.setMaxResults(pageSize);
        }
        List results = queryObject.list();
        transformEntities(transform, results);
        return results;
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:com.algoTrader.entity.PositionDaoBase.java

/**
 * {@inheritDoc}/*www. j a v  a  2 s.com*/
 */
@Override
@SuppressWarnings("unchecked")
public Object findByIdFetched(final int transform, final String queryString, final int id) {
    try {
        Query queryObject = super.getSession(false).createQuery(queryString);
        queryObject.setCacheable(true);
        queryObject.setParameter("id", new Integer(id));
        Set results = new LinkedHashSet(queryObject.list());
        Object result = null;
        if (results.size() > 1) {
            throw new InvalidDataAccessResourceUsageException(
                    "More than one instance of 'com.algoTrader.entity.Position"
                            + "' was found when executing query --> '" + queryString + "'");
        } else if (results.size() == 1) {
            result = results.iterator().next();
        }
        if (transform != TRANSFORM_NONE) {
            result = transformEntity(transform, (Position) result);
        }
        return result;
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}