Error displaying a collection of a contact’s phones and emails

Asked

Viewed 94 times

1

I want to display on a XHTML page (JSF+Primefaces) a list of contacts with phones and emails.

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 console test class 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 presents 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 idea how to solve this problem?

  • It’s a Lily LazyInitializationException, then two ways to solve: change the fetch from the contact phone list to EAGER; and the most "elegant", make a filter (or use a ready one) that initializes the session.

  • thanks, I’ll try to change the fetch.

  • OK, if it works out let me know that I include an answer for you considering the two approaches.

  • opa, I was using two classes @Embeddable for phones and emails. And at the time of select Hibernate did not Join. I switched the two to @Entity mapped with @ Manytoone and put fetch as EAGER. I’m sure there’s a way to do it with collections object (@ Embeddable). But let it go. It’s working. Thank you very much.

  • @fercosmig, please don’t put EAGER in the relationship between Contact and Phone, unless you want at all times that phones come every time you consult for any contact on your system. Overall, this is a bad idea. Choose to make a point query (with HQL or Criteria) to contacts using join fetch to pick up the phones.

  • If I figure out how to do it using the annotated collection as @Embeddable, I’ll change the system. It is a scheduling system, it shows the contact, the phones and the emails, always.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.