users@glassfish.java.net

Lazy collections and remote client

From: <glassfish_at_javadesktop.org>
Date: Thu, 29 Mar 2007 18:07:16 PST

Hi

I have a problem with a lazy collection accessed through a stateless facade by a remote client.
If i dont initialize the collection in the facade (by calling iterator().next() for instance), I get a TOPLINK-7242 error saying "An attempt was made to traverse a relationship using indirection that had a null Session. This often occurs when an entity with an uninstantiated LAZY relationship is serialized and that lazy relationship is traversed after serialization. To avoid this issue, instantiate the LAZY relationship prior to serialization."

The guilty entities:
public class Ile implements Serializable {

    @OneToMany(mappedBy="ile",fetch=FetchType.LAZY)
    @OrderBy(value="nom")
    private List<Commune> communes;
...
public class Commune implements Serializable {
    @ManyToOne()
    @JoinColumn(name="ID_ILE")
    private Ile ile;
...

the bad session facade:
@Stateless
public class IleServicesBean implements IleServicesRemote {
     public List<Commune> getCommunes(int idIle) {
        Ile ile = em.find(Ile.class,idIle);
        
        List<Commune> communes = ile.getCommunes();
        //communes.iterator().next(); //Works, the collections get initialiazed
        
        return communes;
    }

And the ugly client I run through the ACC (via Netbeans or JWS):
public class TestClient {
    @EJB
    private static IleServicesRemote ilesService;

  public static void main(String[] args) {

 for (Commune co:ilesService.getCommunes(ile.getId())){
                System.out.println("\t"+co);
            }
}

In the session facade, the only way i found to correctly initialize the lazy collection is to retrieve the first object. Is it the correct behavior, or am i missing something ?
[Message sent by forum member 'ygillet' (ygillet)]

http://forums.java.net/jive/thread.jspa?messageID=210542