Olá, pessoal!
Preciso tratar duas coleções em uma mesma sessão, então me recomendaram o uso do Open Session in View no Hibernate.
Estou um pouco confuso.
Aqui está meu filtro, referente ao Open Session in View, exatamente como está na documentação do Hibernate:
public class HibernateSessionRequestFilter implements Filter {
private static Log log = LogFactory.getLog(HibernateSessionRequestFilter.class);
private SessionFactory sf;
public void init(FilterConfig filterConfig) throws ServletException {
log.debug("Initializing filter...");
log.debug("Obtaining SessionFactory from static HibernateUtil singleton");
sf = HibernateUtil.getSessionFactory();
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
log.debug("Starting a database transaction");
sf.getCurrentSession().beginTransaction();
// Call the next filter (continue request processing)
chain.doFilter(request, response);
// Commit and cleanup
log.debug("Committing the database transaction");
sf.getCurrentSession().getTransaction().commit();
} catch (StaleObjectStateException staleEx) {
log.error("This interceptor does not implement optimistic concurrency control!");
log.error("Your application will not work until you add compensation actions!");
// Rollback, close everything, possibly compensate for any permanent changes
// during the conversation, and finally restart business conversation. Maybe
// give the user of the application a chance to merge some of his work with
// fresh data... what you do here depends on your applications design.
throw staleEx;
} catch (Throwable ex) {
// Rollback only
ex.printStackTrace();
try {
if (sf.getCurrentSession().getTransaction().isActive()) {
log.debug("Trying to rollback database transaction after exception");
sf.getCurrentSession().getTransaction().rollback();
}
} catch (Throwable rbEx) {
log.error("Could not rollback transaction after exception!", rbEx);
}
// Let others handle it... maybe another interceptor for exceptions?
throw new ServletException(ex);
}
}
public void destroy() {
//throw new UnsupportedOperationException("Not supported yet.");
}
}
E aqui está o meu HibernateUtil:
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
return new AnnotationConfiguration()
.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect")
.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver")
.setProperty("hibernate.connection.url", "jdbc:postgresql://localhost:5432/coopconvenios")
.setProperty("hibernate.connection.username", "postgres")
.setProperty("hibernate.connection.password", "123456")
.setProperty("hibernate.show_sql", "false")
.setProperty("hibernate.format_sql", "true")
.setProperty("hibernate.hbm2ddl.auto", "update")
.setProperty("hibernate.current_session_context_class", "thread")
.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider")
.setProperty("hibernate.c3p0.acquire_increment", "1")
.setProperty("hibernate.c3p0.idle_test_period", "100")
.setProperty("hibernate.c3p0.min_size", "5")
.setProperty("hibernate.c3p0.max_size", "25")
.setProperty("hibernate.c3p0.max_statements", "50")
.setProperty("hibernate.c3p0.timeout", "1800")
.addAnnotatedClass(Titular.class)
.addAnnotatedClass(Dependente.class)
.addAnnotatedClass(Orgao.class)
.addAnnotatedClass(Instituicao.class)
.addAnnotatedClass(Alocacao.class)
.addAnnotatedClass(Endereco.class)
.addAnnotatedClass(PreferenciaCobranca.class)
.addAnnotatedClass(Conta.class)
.addAnnotatedClass(Prestador.class)
.addAnnotatedClass(Plano.class)
.addAnnotatedClass(FaixaEtaria.class)
.addAnnotatedClass(ContratoDePlano.class)
.addAnnotatedClass(ContratoDeServico.class)
.addAnnotatedClass(TipoContrato.class)
.addAnnotatedClass(Movimento.class)
.buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
O que não ficou claro pra mim é como utilizar o filtro. Estou um pouco confuso. Como associo o HibernateUtil com o filtro?
Espero que possam me ajudar!
Obrigado!
