开发者

CriteriaQuery "where" gets lost when "orderBy" is applied

I am learning how to use CriteriaQuery in JPA 2.0 and hit a snag. My application builds a query with interactive input from the user. When the column headers are clicked, an orderBy term is applied. When they type into a text field right under the column header, a filter is applied to that column.

Doing this, I can use the orderBy() method and it works, for any number of terms. I can use the where() method and it works. When I use both together, the where() predicate gets ignored.

If I reve开发者_StackOverflow中文版rse the order of what gets called to the CriteriaQuery object it makes no difference. If a sort is applied the filter no longer works.

I have tried looking for examples on the web where both methods are used on the same query but haven't found one.

Any idea what I could be doing wrong? Is there something I missed in the documentation?

This is using Derby via eclipselink 2.2.0.


Sorry everyone I found the bug. It was not a problem in the JPA at all -- a subtle problem with an if statement caused the filter terms to be erased when a sort term was introduced. Let's hear it for test code.

Here was the test code I was going to post. It works. Textbook.

    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Location> cq = qb.createQuery(Location.class);        
    Root<Location> root = cq.from(Location.class);

    // test case

    cq.where(qb.like(qb.upper(root.get(Location_.name)), "C%"));
    cq.orderBy(qb.desc(root.get(Location_.id)));
    TypedQuery<Location> q = em.createQuery(cq);
    q.setFirstResult(0);
    q.setMaxResults(20);
    list = q.getResultList();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜