Hi,
Cursor provides a "point" so that query will execute after that.
I don't think you can achieve the same thing with query.setFirstResult()

Below is code I ran based on consulting JDO example and JPA javadocs. Hope
this helps

EntityManager em = ...
Query query = em.createQuery(queryString);
query.setMaxResults(pageSize);

List<User> users = query.getResultList();

if (users.size() == pageSize) {
    Cursor cursor = JPACursorHelper.getCursor(users);
    if (cursor != null) {
        String cursorString = cursor.toWebSafeString();
        // store cursorString for later use
    }
}

....

Query query = em.createQuery(queryString); // same query above
query.setMaxResults(pageSize);
Cursor cursor = Cursor.fromWebSafeString(cursorString);
if (cursor != null) {
    query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
}
List<User> users = query.getResultList();
...

On Thu, May 6, 2010 at 5:27 AM, fvisticot <fvisti...@gmail.com> wrote:

> I do not see any documentation regarding cursor support with JPA.
>
> Is the following code the correct way to use cursors ?
> In this sample code, i will retrieve users from 300 to 320
> (query.setMaxResults(20);       query.setFirstResult(300);)
>
> The test is OK with the Local Eclipse GAE plugin
>
> @Test
>        public void getUsersCursorTest() {
>                EntityManagerFactory emf = null;
>                EntityManager em = null;
>                emf = AppEntityManagerFactory.get();
>                try {
>                        em = emf.createEntityManager();
>                        Query query = em.createNamedQuery("User.findAll");
>                        query.setMaxResults(20);
>                        query.setFirstResult(300);
>                        List<User> users = query.getResultList();
>                        _logger.info("UsersWithMax: " + users.size());
>                        for (User user: users) {
>                                _logger.info("User: " + user);
>                        }
>                } finally {
>                        if (em != null)
>                                em.close();
>                }
>        }
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine for Java" group.
> To post to this group, send email to
> google-appengine-j...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-appengine-java+unsubscr...@googlegroups.com<google-appengine-java%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-appengine-java?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to