0

I'm trying to use jpa with the app engine datastore.

Persistence is fine, though I can't seem to get any of the objects I persisted in the db.

Here is the method I made :

public List getAllBooks() {

  EntityManager em = EMF.get().createEntityManager();

  Query query = em.createQuery("select b from Book b");

  List<Book> books = (List<Book>) query.getResultList();

  em.close();

  return books;

}

The odd thing is when I debug and look at my list of results, there is some things I don't understand :

1 - my List seems to be actually a StreamingQueryResult, and its size is -1

2 - when I searched in this StreamingQueryResult, I realized the 3 books I have in my db are actually in an arraylist called resolvedPojos which is in LazyResult, but I don't know how to get them.

Has anyone got an idea ?

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130

2 Answers2

0

You will have to make an explicit call on the lazy collection in order to initialize it (common practice is to call .size() for this purpose).

I think "this link" addresses this issue. "This link" maybe also.

Community
  • 1
  • 1
NicholasKarl
  • 253
  • 3
  • 12
  • I tried to call size(), but it doesn't seem to do the trick. What I don't get is why would jpa be doing a lazy loading if there is only one entity to load... – user3083103 Dec 09 '13 at 16:11
0

Simply passing some proxy "List" back to the rest of your app is (IMHO) bad practice; you expose yourself to whatever oddities that List has in terms of lazy loading. The fact that you are closing the EM means you can't tolerate lazy loading. So just create your own List and copy the query results list into it ... before closing the EM, and while doing that if wanting children of the query results then touch the related fields (so they are loaded), or make sure they are in the fetch plan (DataNucleus extension) of the query.

DataNucleus
  • 15,497
  • 3
  • 32
  • 37