I want to display a list of contacts with phones and emails on an XHTML (JSF + PrimeFaces) page.
I can display everything I want in a test class:
package com.fercosmig.testes;
import java.util.List;
import javax.persistence.EntityManager;
import com.fercosmig.contato.model.Contato;
import com.fercosmig.contato.model.Email;
import com.fercosmig.contato.model.Telefone;
import com.fercosmig.contato.repository.ContatoRepository;
import com.fercosmig.util.jpa.JpaUtil;
public class TesteListaContatos {
public static void main(String[] args) {
EntityManager manager = JpaUtil.getEntityManager();
ContatoRepository contatos = new ContatoRepository(manager);
List<Contato> contatosLista = contatos.todos();
for (Contato contato : contatosLista){
System.out.print(contato.getId());
System.out.print(" - ");
System.out.print(contato.getNome());
System.out.print(" - ");
System.out.println(contato.getObservacao());
for (Telefone telefone : contato.getTelefones()){
System.out.print(telefone.getDdd());
System.out.print(" - ");
System.out.print(telefone.getNumero());
System.out.print(" - ");
System.out.print(telefone.getRamal());
System.out.print(" - ");
System.out.print(telefone.getTipo());
System.out.print(" - ");
System.out.println(telefone.getOperadora());
}
for (Email email : contato.getEmails()){
System.out.print(email.getEmail());
System.out.print(" - ");
System.out.println(email.getTipo());
}
}
}
}
The result of this test class in the console is:
1 - Alfredo Neves - colega da faculdade
11 - 955555555 - null - CELULAR - VIVO
11 - 12341234 - null - COMERCIAL - null
11 - 23452345 - null - RESIDENCIAL - null
[email protected] - PARTICULAR
2 - Rodolfo Complefica - colega de trabalho
11 - 956785678 - null - CELULAR - TIM
11 - 89078907 - null - RESIDENCIAL - null
[email protected] - PARTICULAR
I want to put this on the page
<h:form id="frm1">
<h:dataTable value="#{consultaContatoBean.contatos}" var="contato"
border="1" cellspacing="0" cellpadding="2">
<h:column>
<f:facet name="header">
<h:outputText value="Id" />
</f:facet>
<h:outputText value="#{contato.id}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Nome" />
</f:facet>
<h:outputText value="#{contato.nome}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Observação" />
</f:facet>
<h:outputText value="#{contato.observacao}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Telefones" />
</f:facet>
<h:panelGroup>
<ui:repeat var="telefone" value="#{contato.telefones}">
<h:outputText value="#{telefone}" />
</ui:repeat>
</h:panelGroup>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Emails" />
</f:facet>
<h:panelGroup>
<ui:repeat var="email" value="#{contato.emails}">
<h:outputText value="#{email}" />
</ui:repeat>
</h:panelGroup>
</h:column>
</h:dataTable>
</h:form>
but displays the error below:
failed to lazily initialize a collection of role: com.fercosmig.contato.model.Contato.telefones, could not initialize proxy - no Session
The error is in the repeat structure that shows all phone numbers and all emails.
Does anyone have any ideas for solving this problem?