Hibernate cache na sessao

4 respostas
lucifeler

Boa tarde,
estou utilizando hibernate com annotations so que em uma tela em que eu faço uma consulta para listar os exames, o hibernate esta guardando no cache da sessao causando inconsistencia em consultas posteriores.

A solução que tive foi dar um session.clear(); ou session.envict(Exame.class);

Gostaria de saber se tem como configurar o hibernate para que ele nao guardasse os dados no cache da sessão.

public List<Exame> busca(Exame exame, Long pageSize, 
            Long pageNumber) throws DAOException {
        try {

            Session session = DAOSession.currentSession();
            
            session.clear();
            
            Criteria criteria = session.createCriteria(Exame.class);
            
            Long startRow = pageSize * (pageNumber -1) ;

            if(Util.stringValida(exame.getNomeExame())) {
                criteria.add(Restrictions.like("nomeExame", exame.getNomeExame(), MatchMode.ANYWHERE ));
            }

            if(Util.stringValida(exame.getCentroCustoServico())) {
                criteria.add(Restrictions.eq("centroCustoServico", exame.getCentroCustoServico()));
            }

            if(exame.getCodigoExame() > -1){
            	criteria.add(Restrictions.eq("codigoExame", exame.getCodigoExame()));
            }
            criteria.add(Restrictions.eq("situacao", Boolean.valueOf(exame.isSituacao())));

            
            //paginação
            criteria.addOrder(Order.asc("nomeExame"));
            criteria.setFirstResult(startRow.intValue());
            criteria.setMaxResults(pageSize.intValue());
            
            
            return criteria.list();

4 Respostas

nicholas.bittencourt

Como esta implementado o metodo DAOSession.currentSession()?

lucifeler

estou implemtando dessa maneira

package br.com.cultura.sisint.dao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;


/**
 * Classe utilitaria para manipulacao das sessoes com o Hibernate.
 * 
 * 
 */
public class DAOSession {
	
	private static SessionFactory SF;

	static {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		try {

			SF = cfg.configure().buildSessionFactory();
			
		} 
		catch (HibernateException e) {
		    throw new RuntimeException(e.getMessage());
		}
	}

	public static final ThreadLocal session = new ThreadLocal();

	/**
	 * Cria uma nova sessao.
	 */
	@SuppressWarnings("unchecked")	
	public static void openSession() {
		try {
			session.set(SF.openSession());
		} 
		catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	
	/**
	 * Retorna a sessao atual.
	 * @throws RuntimeException	se o método for chamado e não existir uma sessão.
	 */	
	@SuppressWarnings("unchecked")
	public static Session currentSession() {
		Session s = (Session) session.get();
        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = SF.openSession();
            session.set(s);
        }

		return s;
	}

	/**
	 * Fecha a sessao atual.
	 */	
	@SuppressWarnings("unchecked")	
	public static void closeSession() {
		try {
			Session s = currentSession();
			session.set(null);
			s.flush();
			s.close();
		} 
		catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	public static void hsqlCleanup(Session s) {
	    try {
	        s.connection().createStatement().execute("SHUTDOWN");
	    } catch (Exception e) {
	    }
	}

}
A

Tenta usar a seguinte linha no arquivo de configuração do Hibernate:

marlon_divino

Caracas… eu também estou com esse problema!!!
Alguem conhece uma solução?

Criado 4 de junho de 2007
Ultima resposta 1 de set. de 2009
Respostas 4
Participantes 4