Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named query and pagination

I am trying to use named query with pagination of elements, but I am not a database expert and the answers I found didn't help much, will be thankful for some help, code :

@Entity
@NamedQueries({
    @NamedQuery(name = "Object.byName", query = "select a from OBJECT a where a.name=?"),

})

using : findByNamedQuery("Object.byName", a);

I know I need to use setFirstResult(x); and setMaxResults(y); but how to use them with findByNamedQuery.

like image 291
seismael Avatar asked Jan 26 '11 21:01

seismael


1 Answers

You need to work with the session/entity manager directly:

Query q = entityManager.createNamedQuery("Object.byName");
q.setFirstResult(x);
q.setMaxResults(pageSize);
//set the parameters here
return q.list();

This is the JPA syntax, hibernate's is almost the same.

like image 84
Bozho Avatar answered Oct 04 '22 04:10

Bozho