views:

136

answers:

1

Hi

I use Google App Engine with datanucleus and JPA. I'm having a real hard time grasping how I'm supposed to read stuff from data store and pass it to JSP. If I load a list of POJOs with entitymanager and pass it to JSP, it crashes to org.datanucleus.exceptions.NucleusUserException: Object Manager has been closed.

I understand why this is happening. Obviously because I fetch the list, close the entity manager and pass it to JSP, at which point it will fail because the list is lazy. How do I make the list NOT lazy without resorting to hacks like calling size() or something like that?

Here is what I'm trying to do:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setAttribute("parties", getParties());
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/parties.jsp");
    dispatcher.forward(req, resp);
}

private List<Party> getParties(){
    EntityManager em = entityManagerProvider.get();
    try{
        Query query = em.createQuery("SELECT p FROM Party p");
        return query.getResultList();
    }finally{
        em.close();
    }
}
+1  A: 

According to this thread, DataNucleus itself provides mechanisms for reading in of query results when a transaction commits. It's likely just not yet implemented in the plugin yet. This is reported in Issue 24 and until this issue get solved, my understanding is that you'll have to either call size() to load the list or to use the OpenEntityManagerInView pattern.

Pascal Thivent
Thanks, although I'm going to use new LinkedList(results) or new ArrayList(results) on this, because it feels conceptually more right to me than just calling size(). Atleast this way I can control what kind of collection I actually get from the query.I could use OpenEntityMangerInView pattern, but it feels so overkill solution for such a simple problem. Maybe if the software grows in size.
palto