2011-02-25

使用 JPA 得到的 result 是 lazy load?

在 Google App Engine 環境使用 JPA,程式碼如下:
EntityManager em = EMF.get().createEntityManager();
Query q = em.createQuery("select u from " + User.class.getName() + " u");
q.setFirstResult(first);
q.setMaxResults(max);
List<user> list = q.getResultList();
log.debug("Find {} users", list.size());
em.close();

如果將倒數第二行讀取 list size 的那行 log 註解掉,則在 close 之後讀取 list 會出現以下的錯誤:
Object Manager has been closed
org.datanucleus.exceptions.NucleusUserException: Object Manager has been closed
 at org.datanucleus.ObjectManagerImpl.assertIsOpen(ObjectManagerImpl.java:3876)
 at org.datanucleus.ObjectManagerImpl.getFetchPlan(ObjectManagerImpl.java:376)
 at org.datanucleus.store.query.Query.getFetchPlan(Query.java:497)
 at org.datanucleus.store.appengine.query.DatastoreQuery$6.apply(DatastoreQuery.java:636)
 at org.datanucleus.store.appengine.query.DatastoreQuery$6.apply(DatastoreQuery.java:635)
 at org.datanucleus.store.appengine.query.LazyResult.resolveNext(LazyResult.java:94)
 at org.datanucleus.store.appengine.query.LazyResult$LazyAbstractListIterator.computeNext(LazyResult.java:215)
 at org.datanucleus.store.appengine.query.AbstractIterator.tryToComputeNext(AbstractIterator.java:132)
 at org.datanucleus.store.appengine.query.AbstractIterator.hasNext(AbstractIterator.java:127)
 at org.datanucleus.store.appengine.query.LazyResult$AbstractListIterator.hasNext(LazyResult.java:169)
 at org.apache.taglibs.standard.tag.common.core.ForEachSupport$SimpleForEachIterator.hasNext(ForEachSupport.java:116)
 at org.apache.taglibs.standard.tag.common.core.ForEachSupport.hasNext(ForEachSupport.java:137)
 at javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:262)

經過 Google 研究之後,兇手不不知道是誰,GAE?DataNucleus ?JPA?目前無解!

沒有留言:

張貼留言