Boa tarde,
Tenho um projeto, que utilizo : Hibernate, Spring Security, Jsf, Anotations etc.
Ao tentar salvar, listar, deletar objetos no banco me e informado que a sessão do hibernate foi fechada.
Estou utilizando Hibernate + Filter, veja como está :
Entidade como exemplo :
[code]Entity
public class Omissao implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "id_omissao", sequenceName = "ID_OMISSAO")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "id_omissao")
private Integer id_omissao;
@Temporal(TemporalType.DATE)
private Date data_cad_omissao;
@Temporal(TemporalType.DATE)
private Date data_ocor_omissao;
private Double quant_omissao;
@Temporal(TemporalType.TIME)
private Date hora_omissao;
private String mtv_omissao;
private String pto_omissao;
private String obs_omissao;
private boolean pdf_omissao;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "usuario", nullable = false)
private Usuario usuario;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "empresa", nullable = false)
private Empresa empresa = new Empresa();
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "veiculo", nullable = false)
private Veiculo veiculo = new Veiculo();
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "linha", nullable = false)
private Linha linha = new Linha();
getters e setters
[/code]
Hibernate Filter
[code]import br.com.omissoesWeb.util.HibernateUtil;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.hibernate.SessionFactory;
public class HibernateFilter implements javax.servlet.Filter {
private SessionFactory sf;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse sevletResponse, FilterChain chain)
throws IOException, ServletException {
try {
this.sf.getCurrentSession().beginTransaction();
chain.doFilter(servletRequest, sevletResponse);
this.sf.getCurrentSession().getTransaction().commit();
this.sf.getCurrentSession().close();
} catch (Throwable ex) {
try {
if (this.sf.getCurrentSession().getTransaction().isActive()) {
this.sf.getCurrentSession().getTransaction().rollback();
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
@Override
public void init(FilterConfig config) throws ServletException {
this.sf = HibernateUtil.getSessionFactory();
}
}[/code]
Hibernate Util
[code]import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
@SuppressWarnings(“deprecation”)
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
} catch (Throwable e) {
System.out
.println("Criação inicial do objeto SessionFactory falhou. Erro :"
+ e);
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}[/code]
web.xml
Filtro
<filter>
<filter-name>conexaoFilter</filter-name>
<filter-class>br.com.omissoesWeb.filter.HibernateFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>conexaoFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Spring Security Filter
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Alguem sabe me dizer o porquê de acontecer o erro de Sessão Fechada do Hibernate.
Obs : Já li varios post referente ao assunto, mas não consegui resolve-lo.