Boa tarde, Comunidade.
Sou meio iniciante nesse conceito de persistência, mas mesmo com algumas dificuldades estou gostando muito.
Estou mudando o conceito de uma aplicação que to desenvolvendo para usar o Hibernate Filter (vi num livro), mas não estou conseguindo colocar para funcionar.
Minha classe “ConexaoHibernateFilter” está no pacote “filter” do meu projeto.
Ela está com o seguinte conteúdo:
package filter;
import util.HibernateUtil;
import java.util.Collection;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.hibernate.Filter;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.engine.FilterDefinition;
public class ConexaoHibernateFilter implements Filter{
private SessionFactory sf;
public void init(FilterConfig config) throws ServletException {
this.sf = HibernateUtil.getSessionFactory();
}
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain chain) throws ServletException{
try{
this.sf.getCurrentSession().beginTransaction();
chain.doFilter(servletRequest, servletResponse);
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();
}
throw new ServletException(ex);
}
}
public void destroy(){}
@Override
public String getName() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public FilterDefinition getFilterDefinition() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Filter setParameter(String string, Object o) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Filter setParameterList(String string, Collection clctn) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Filter setParameterList(String string, Object[] os) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void validate() throws HibernateException {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Fiz o mapeamento da classe no web.xml, de modo que ficou do seguinte jeito
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<filter>
<filter-name>conexaoFilter</filter-name>
<filter-class>filter.ConexaoHibernateFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>conexaoFilter</filter-name>
<url-pattern>*.jsf</url-pattern>
</filter-mapping>
</web-app>
é aí que vem o problema!!
Quando dou um “run” no meu projeto, aparece o seguinte erro na inicialização do Filter.
Alguma sugestão? dá impressão que meu método iinit tá com pau.
GRAVE: WebModule[/CRM]PWC1270: Exception starting filter conexaoFilter
java.lang.InstantiationException
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:124)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4625)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:5316)
at com.sun.enterprise.web.WebModule.start(WebModule.java:500)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:917)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:901)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:755)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1980)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1630)
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:100)
at org.glassfish.internal.data.EngineRef.start(EngineRef.java:130)
at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:269)
at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:286)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:461)
at com.sun.enterprise.v3.server.ApplicationLoaderService.processApplication(ApplicationLoaderService.java:364)
at com.sun.enterprise.v3.server.ApplicationLoaderService.postConstruct(ApplicationLoaderService.java:208)
at com.sun.hk2.component.AbstractCreatorImpl.inject(AbstractCreatorImpl.java:131)
at com.sun.hk2.component.ConstructorCreator.initialize(ConstructorCreator.java:91)
at com.sun.hk2.component.AbstractCreatorImpl.get(AbstractCreatorImpl.java:82)
at com.sun.hk2.component.SingletonInhabitant.get(SingletonInhabitant.java:67)
at com.sun.hk2.component.EventPublishingInhabitant.get(EventPublishingInhabitant.java:139)
at com.sun.hk2.component.AbstractInhabitantImpl.get(AbstractInhabitantImpl.java:76)
at com.sun.enterprise.v3.server.AppServerStartup.run(AppServerStartup.java:243)
at com.sun.enterprise.v3.server.AppServerStartup.start(AppServerStartup.java:135)
at com.sun.enterprise.glassfish.bootstrap.GlassFishImpl.start(GlassFishImpl.java:79)
at com.sun.enterprise.glassfish.bootstrap.GlassFishMain$Launcher.launch(GlassFishMain.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.enterprise.glassfish.bootstrap.GlassFishMain.main(GlassFishMain.java:97)
at com.sun.enterprise.glassfish.bootstrap.ASMain.main(ASMain.java:55)
Caused by: java.lang.ClassCastException: filter.ConexaoHibernateFilter cannot be cast to javax.servlet.Filter
at com.sun.enterprise.web.WebContainer.createFilterInstance(WebContainer.java:718)
at com.sun.enterprise.web.WebModule.createFilterInstance(WebModule.java:1970)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:255)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:120)
... 31 more
por via das dúvidas, cou colocar aqui minha classe HibernateUtil que está no pacote util:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Hibernate Utility class with a convenient method to get Session Factory object.
*
* @author root
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory(){
try{
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
return cfg.buildSessionFactory();
} catch (Throwable e){
System.out.println("Falhou. Erro " +e);
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Muito obrigado! toda ajuda será bem vinda!!
Vlw