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.gov.jfrj.siga.dp.dao.CpDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public CpServico consultarPorSigla(final CpServico o) {
    final Query query = getSessao().getNamedQuery("consultarPorSiglaCpServico");
    query.setString("siglaServico", o.getSiglaServico());
    query.setLong("idServicoPai", o.getCpServicoPai() == null ? 0 : o.getCpServicoPai().getIdServico());

    // Renato: Comentei a linha abaixo pois  nao entendi porque foi feito
    // dessa forma.
    // query.setFlushMode(FlushMode.MANUAL);

    query.setCacheable(true);
    query.setCacheRegion(CACHE_QUERY_HOURS);

    final List<CpServico> l = query.list();
    if (l.size() != 1)
        return null;
    return l.get(0);
}

From source file:br.gov.jfrj.siga.dp.dao.CpDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public CpServico consultarCpServicoPorChave(String chave) {
    // Cria uma cache region especifica da classe para garantir que os
    // objetos armazenados por uma aplicacacao seja recuperados por outra.
    // Isso causa ClassCastException.
    final String cRegion = CACHE_CORPORATIVO + "_" + this.getClass().getSimpleName();
    Cache cache = CacheManager.getInstance().getCache(cRegion);
    if (cache == null) {
        CacheManager manager = CacheManager.getInstance();
        manager.addCache(cRegion);/*  ww w.j a  v a2 s .  c o  m*/
        cache = manager.getCache(cRegion);
        CacheConfiguration config;
        config = cache.getCacheConfiguration();
        config.setEternal(true);
        config.setMaxElementsInMemory(10000);
        config.setOverflowToDisk(false);
        config.setMaxElementsOnDisk(0);
    }
    Element element;
    if ((element = cache.get(chave)) != null) {
        return (CpServico) element.getValue();
    }

    StringBuilder sb = new StringBuilder(50);
    boolean supress = false;
    for (int i = 0; i < chave.length(); i++) {
        final char ch = chave.charAt(i);
        if (ch == ';') {
            sb.append('-');
            supress = false;
            continue;
        }
        if (ch == ':') {
            supress = true;
            continue;
        }
        if (!supress)
            sb.append(ch);
    }
    String sigla = sb.toString();

    final Query query = getSessao().getNamedQuery("consultarPorSiglaStringCpServico");
    query.setString("siglaServico", sigla);

    query.setCacheable(true);
    query.setCacheRegion(CACHE_QUERY_HOURS);

    final List<CpServico> l = query.list();
    if (l.size() != 1)
        return null;

    // Forca a carga de algums campos para garantir o lazy load.
    CpServico srv = (CpServico) l.get(0).getImplementation();
    Object o1 = srv.getCpServicoPai().getDescricao();
    Object o2 = srv.getCpTipoServico().getDscTpServico();

    cache.put(new Element(chave, srv));
    return l.get(0);
}

From source file:br.gov.jfrj.siga.dp.dao.CpDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<CpOrgaoUsuario> consultarPorFiltro(final CpOrgaoUsuarioDaoFiltro o, final int offset,
        final int itemPagina) {
    try {/*from w w  w. ja v  a2 s .  c o  m*/
        final Query query = getSessao().getNamedQuery("consultarPorFiltroCpOrgao");
        if (offset > 0) {
            query.setFirstResult(offset);
        }
        if (itemPagina > 0) {
            query.setMaxResults(itemPagina);
        }
        String s = o.getNome();
        if (s != null)
            s = s.replace(' ', '%');
        query.setString("nome", s);

        query.setCacheable(true);
        query.setCacheRegion(CACHE_QUERY_HOURS);

        final List<CpOrgaoUsuario> l = query.list();
        return l;
    } catch (final NullPointerException e) {
        return null;
    }
}

From source file:br.gov.jfrj.siga.dp.dao.CpDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public CpOrgaoUsuario consultarPorSigla(final CpOrgaoUsuario o) {
    final Query query = getSessao().getNamedQuery("consultarSiglaOrgaoUsuario");
    query.setString("sigla", o.getSiglaOrgaoUsu());

    query.setCacheable(true);
    query.setCacheRegion(CACHE_QUERY_HOURS);

    final List<CpOrgaoUsuario> l = query.list();
    if (l.size() != 1)
        return null;
    return l.get(0);
}

From source file:br.gov.jfrj.siga.dp.dao.CpDao.java

License:Open Source License

public int consultarQuantidade(final CpOrgaoUsuarioDaoFiltro o) {
    try {/*ww w . j av a2s  .  c o m*/
        final Query query = getSessao().getNamedQuery("consultarQuantidadeCpOrgao");
        String s = o.getNome();
        if (s != null)
            s = s.replace(' ', '%');
        query.setString("nome", s);

        query.setCacheable(true);
        query.setCacheRegion(CACHE_QUERY_HOURS);

        final int l = ((Long) query.uniqueResult()).intValue();
        return l;
    } catch (final NullPointerException e) {
        return 0;
    }
}

From source file:br.gov.jfrj.siga.dp.dao.CpDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<CpAplicacaoFeriado> listarAplicacoesFeriado(final CpAplicacaoFeriado apl) {
    final Query query = getSessao().getNamedQuery("listarAplicacoesFeriado");
    query.setLong("cpOcorrenciaFeriado", apl.getCpOcorrenciaFeriado().getId());

    query.setCacheable(true);
    query.setCacheRegion(CACHE_QUERY_HOURS);

    final List<CpAplicacaoFeriado> l = query.list();
    return l;/*from ww w .  j  av a  2  s .c om*/
}

From source file:br.gov.jfrj.siga.dp.dao.CpDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<DpLotacao> consultarPorFiltro(final DpLotacaoDaoFiltro o, final int offset, final int itemPagina) {
    try {/* w  w  w . j av  a  2 s .  c  o m*/
        final Query query;

        if (!o.isBuscarFechadas())
            query = getSessao().getNamedQuery("consultarPorFiltroDpLotacao");
        else
            query = getSessao().getNamedQuery("consultarPorFiltroDpLotacaoInclusiveFechadas");
        if (offset > 0) {
            query.setFirstResult(offset);
        }
        if (itemPagina > 0) {
            query.setMaxResults(itemPagina);
        }
        query.setString("nome", o.getNome() == null ? "" : o.getNome().replace(' ', '%'));

        if (o.getIdOrgaoUsu() != null)
            query.setLong("idOrgaoUsu", o.getIdOrgaoUsu());
        else
            query.setLong("idOrgaoUsu", 0);

        query.setCacheable(true);
        query.setCacheRegion(CACHE_QUERY_CONFIGURACAO);
        final List<DpLotacao> l = query.list();
        return l;
    } catch (final NullPointerException e) {
        return null;
    }
}

From source file:br.gov.jfrj.siga.dp.dao.CpDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public DpLotacao consultarPorSigla(final DpLotacao o) {
    final Query query = getSessao().getNamedQuery("consultarPorSiglaDpLotacao");
    query.setString("siglaLotacao", o.getSigla());
    if (o.getOrgaoUsuario() != null)
        if (o.getOrgaoUsuario().getIdOrgaoUsu() != null)
            query.setLong("idOrgaoUsu", o.getOrgaoUsuario().getIdOrgaoUsu());
        else//  ww  w. java  2  s.com
            query.setLong("idOrgaoUsu", consultarPorSigla(o.getOrgaoUsuario()).getId());
    else
        query.setLong("idOrgaoUsu", 0);

    query.setCacheable(true);
    query.setCacheRegion(CACHE_QUERY_CONFIGURACAO);
    final List<DpLotacao> l = query.list();
    if (l.size() != 1)
        return null;
    return l.get(0);
}

From source file:br.gov.jfrj.siga.dp.dao.CpDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public DpLotacao consultarPorIdInicial(Class<DpLotacao> clazz, final Long idInicial) {
    final Query query = getSessao().getNamedQuery("consultarPorIdInicialDpLotacao");
    query.setLong("idLotacaoIni", idInicial);

    query.setCacheable(true);
    query.setCacheRegion(CACHE_QUERY_CONFIGURACAO);
    final List<DpLotacao> l = query.list();
    if (l.size() != 1)
        return null;
    return l.get(0);
}

From source file:br.gov.jfrj.siga.dp.dao.CpDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public DpLotacao consultarPorIdInicialInclusiveLotacaoFechada(Class<DpLotacao> clazz, final Long idInicial) {
    final Query query = getSessao().getNamedQuery("consultarPorIdInicialDpLotacaoInclusiveFechada");
    query.setLong("idLotacaoIni", idInicial);

    query.setCacheable(true);
    query.setCacheRegion(CACHE_QUERY_CONFIGURACAO);
    final List<DpLotacao> l = query.list();
    if (l.size() != 1)
        return null;
    return l.get(0);
}