persistence@glassfish.java.net

Lazy load of collection works even after EntityManager is closed?

From: Jon Miller <jemiller_at_uchicago.edu>
Date: Tue, 16 Jan 2007 14:46:57 -0600

Another difference that I noticed between TopLink and Hibernate is that the
following code works with TopLink but not with Hibernate. In the following
example, I have a Location object which has a list of Schedule objects. The
list of schedules isn't being iterated over until after the EntityManager
has been closed. In Hibernate, an exception is thrown, but, it somehow still
works with TopLink. How can that be?

    public void testLazyLoadAfterEntityManagerClosed() {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("Common");
        EntityManager em = emf.createEntityManager();
        Location l = em.find(Location.class, 117);
        logger.info("l = " + l);
        em.close();
        for(Schedule s : l.getSchedules()) {
            logger.info("s = " + s);
        }
        emf.close();
    }

Jon