简体   繁体   中英

empty result list with jpa on datastore

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 ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM