问题
What could cause the CriteriaQuery
orderBy
method stop working?
Here are the implementations:
OpenJPAEntityManager kem = OpenJPAPersistence.cast(entityManager());
kem.getFetchPlan().clearFetchGroups();
kem.getFetchPlan().addFetchGroup("order_search");
CriteriaBuilder builder = kem.getCriteriaBuilder();
CriteriaQuery<Order> query = builder.createQuery(Order.class);
Root<Order> order = query.from(Order.class);
query.select(order);
Predicate main_condition = buildWhereClause(builder, query, order, target_states, orderDate_from, orderDate_to, dueDate_from, dueDate_to, username);
query.where(main_condition);
query.orderBy(builder.desc(order.get("orderDate")));
TypedQuery<Order> q = entityManager().createQuery(query);
if (firstResult != 0)
q.setFirstResult(firstResult);
if (maxResults != 0)
q.setMaxResults(maxResults);
With the pagniation, when there are 15 records and we want 5 records per page, it should be:
-page1- 1 2 3 4 5 -page2- 6 7 8 9 10 -page3- 11 12 13 14 15
But now what we get here is
-page1- 1 2 3 4 5 -page2- 6 7 8 9 10 -page3- 5 4 3 2 1
the last page is always the opposite order of the first page, how come?
回答1:
Nail it, not by myself, this post is quite helpful. I did the same and now it runs like a charm. Basically need to add the primary key in to the order by criteria otherwise the pagination just totally messed.
Spend 4 hours and the solution so simple and tricky. Works on OpenJPA 2.2.2 and Oracle 11g, I guess it is a constant trick since OpenJPA 1.2 from that post.
来源:https://stackoverflow.com/questions/16629384/setfirstresult-and-setmaxresult-doesnt-work-well-with-order-by