Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:32:29+00:00 2026-05-28T01:32:29+00:00

After poking around Stack Overflow I found the following solution for counting problem. My

  • 0

After poking around Stack Overflow I found the following solution for counting problem. My requirement is to get the total number of matching rows, and return the first ten for pagination purposes.

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> cq = cb.createQuery(clazz);
CriteriaQuery<Long> counterCq = cb.createQuery(Long.class);
counterCq.select(cb.count(counterCq.from(clazz)));
Predicate predicate= null;
Predicate predicate1 = null;
Root<T> root = cq.from(clazz);
for (Map.Entry<String, String> e : filters.entrySet()){
    predicate = cb.and(cb.like(root.<String>get(e.getKey()), e.getValue()+ "%"));
}
if(predicate != null){
    cq.where(predicate);
    counterCq.where(predicate);
}
int pn = ( em.createQuery(counterCq).getSingleResult()).intValue();
logger.debug("number of pages is {}", pn);
setRowCount(pn);

if(sortField !=null && !sortField.trim().equals("")){
    if(sortOrder == SortOrder.DESCENDING){
        cq.orderBy(cb.desc(root.get(sortField)));
    } else{
        cq.orderBy(cb.asc(root.get(sortField)));
    }
}

Query q = em.createQuery(cq);
q.setFirstResult(first);
q.setMaxResults(first+ps);
List<T> cats= (List<T>)q.getResultList();

This snippet makes hibernate to through

java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Invalid path: ‘generatedAlias1.title’ [select count(generatedAlias0) from Media as generatedAlias0 where generatedAlias1.title like :param0]

It seems like cq.from(clazz) cannot be applied for the other query.
Now my question: Is there a way to use the same predicate in both queries?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:32:30+00:00Added an answer on May 28, 2026 at 1:32 am

    Your predicate list isn’t assembled correctly. You have to ‘and’ predicates together into a single expression. I also prefer to build my predicate before performing the select for better readability.

    Here’s a refactor of your code to achieve the correct results:

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(clazz);
    Root<T> root = cq.from(clazz);
    
    // build predicate list - conjuction starts us with an empty 'and' predicate
    Predicate predicate = cb.conjunction();
    for (Map.Entry<String, String> e : filters.entrySet()) {
        predicate = cb.and(predicate, cb.like(root.get(e.getKey()), e.getValue() + "%"));
    }
    
    // query total count
    CriteriaQuery<Long> counterCq = cb.createQuery(Long.class);
    counterCq.select(cb.count(root)).where(predicate);
    
    int pn = (em.createQuery(counterCq).getSingleResult()).intValue();
    logger.debug("number of pages is {}", pn);
    setRowCount(pn);
    
    // query results
    cq.select(root).where(predicate);
    
    if(sortField !=null && !sortField.trim().equals("")) {
        if(sortOrder == SortOrder.DESCENDING) {
            cq.orderBy(cb.desc(root.get(sortField)));
        }
        else {
            cq.orderBy(cb.asc(root.get(sortField)));
        }
    }
    
    TypedQuery<T> q = em.createQuery(cq);
    q.setFirstResult(first);
    q.setMaxResults(first+ps);
    List<T> list = q.getResultList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After over a day of poking around with this problem I will see if
Been poking around google and haven't found any like what I'm after. so what
After spending many hours poking around trying to get an ASP .NET AJAX proxy
I have a partly inherited web application in PHP and after poking around with
After much head scratching and poking around I've come to a dead end. I'm
After reading the Head First Design Patterns book and using a number of other
After lots of attempts and search I have never found a satisfactory way to
While poking around the questions, I recently discovered the assert keyword in Java. At
A while ago I was poking around with SQLite, trying to port some of
Ok, as I was poking around with building a custom enumerator, I had noticed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.