Ola bom dia,
Tenho um filtro configurado para open session do hibernate seguindo alguns passos que achei aqui no forum, porem qdo carrego um list dentro de um <p:dialog> ocorre o erro:
11:35:14 ERROR [LazyInitializationException] failed to lazily initialize a collection of role: br.com.casavet.dominio.Fornecedor.contato, no session or session was closed
Já li documentação do proprio Hibernate, fiz modificações no filtro e utilizei outros modelos porem sem sucesso. O Bean ta como @SessionScoped.
Filter
[code]
@WebFilter("/HibernateSessionFilter")
public class HibernateSessionFilter implements Filter {
public HibernateSessionFilter() { }
public void destroy() { }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HibernateUtil.openSession();
try {
HibernateUtil.currentSession().beginTransaction();
chain.doFilter(request, response);
HibernateUtil.currentSession().getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
throw new ServletException(e);
} finally {
HibernateUtil.closeCurrentSession();
}
}
public void init(FilterConfig fConfig) throws ServletException {}
}[/code]
HibernateUtil
[code]public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ThreadLocal<Session> sessions = new ThreadLocal<Session>();
static {
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration.configure();
sessionFactory = configuration.buildSessionFactory();
}
public static Session openSession(){
Session session = sessionFactory.openSession();
sessions.set(session);
return sessions.get();
}
public static Session currentSession(){
return sessions.get();
}
public static void closeCurrentSession(){
sessions.get().close();
sessions.set(null);
}
public static void clearSession(){
sessions.get().clear();
}
public static void flushSession(){
sessions.get().flush();
}
public static void rollback(){
sessions.get().getTransaction().rollback();
sessions.get().getTransaction().begin();
}
} [/code]
Web.xml
<filter>
<display-name>HibernateSessionFilter</display-name>
<filter-name>HibernateSessionFilter</filter-name>
<filter-class>br.com.casavet.filter.HibernateSessionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HibernateSessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Hibernate.cfg.xml
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/casavet</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
Fornecedor
@OneToMany(cascade = CascadeType.ALL, mappedBy = "fornecedorId", fetch = FetchType.LAZY)
private List<Contato> contato;
Contato
@JoinColumn(name = "fornecedorId", referencedColumnName = "fornecedorId")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Fornecedor fornecedorId;
Datatable Fornecedor
[code] <p:dataTable id=“fr” var=“fr” value="#{fornecedorBean.listaFornecedor}"
paginator=“true” rows=“10” paginatorPosition=“bottom”
rowsPerPageTemplate=“10,15,20”
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" >
<p:column headerText="CNPJ">
#{fr.fornecedorCNPJ}
</p:column>
<p:column headerText="Nome">
#{fr.fornecedorNome}
</p:column>
<p:column headerText="Contatos" style="width:60px">
<p:commandLink action="#{fornecedorBean.abrirContato}"
oncomplete="telaContato.show()"
update="@form" async="true">
<span class="ui-icon ui-icon-person"></span>
</p:commandLink>
</p:column>
</p:dataTable>[/code]
Dialog Datatable Contatos
[code]<p:dialog id=“addContatoDialog” modal=“true”
header=“Casa Vet: Lista Contatos - Fornecedor: #{fornecedorBean.fornecedor.fornecedorNome}”
widgetVar=“telaContato” width=“600”>
<p:dataTable id="con" var="con" value="#{fornecedorBean.fornecedor.contatos}"
paginator="true" rows="10" paginatorPosition="bottom"
rowsPerPageTemplate="10,15,20"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" >
<p:column headerText="Nome">
<h:outputText value="#{con.contatoNome}" />
</p:column>
</p:dataTable>
</p:dialog> [/code]
Preciso aprender usar isto, se alguem puder dar uma luz, vlws.
Fábio