开发者

How to iterate JPA collections in Google App engine

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 withou开发者_如何学编程t 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();
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜