How to iterate JPA collections in Google App engine

Posted by palto on Stack Overflow See other posts from Stack Overflow or by palto
Published on 2010-05-31T19:11:13Z Indexed on 2010/05/31 19:13 UTC
Read the original article Hit count: 207

Filed under:
|
|

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();
    }
}

© Stack Overflow or respective owner

Related posts about jpa

Related posts about datanucleus