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

  • SEARCH
  • Home
  • 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 4082914
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:14:48+00:00 2026-05-20T18:14:48+00:00

I am implementing Advanced Search kind of functionality for an Entity in my system

  • 0

I am implementing “Advanced Search” kind of functionality for an Entity in my system such that user can search that entity using multiple conditions(eq,ne,gt,lt,like etc) on attributes of this entity. I am using JPA’s Criteria API to dynamically generate the Criteria query and then using setFirstResult() & setMaxResults() to support pagination. All was fine till this point but now I want to show total number of results on results grid but I did not see a straight forward way to get total count of Criteria query.
This is how my code looks like:

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Brand> cQuery = builder.createQuery(Brand.class);
Root<Brand> from = cQuery.from(Brand.class);
CriteriaQuery<Brand> select = cQuery.select(from);
.
.
//Created many predicates and added to **Predicate[] pArray**
.
.
select.where(pArray);
// Added orderBy clause
TypedQuery typedQuery = em.createQuery(select);
typedQuery.setFirstResult(startIndex);
typedQuery.setMaxResults(pageSize);
List resultList = typedQuery.getResultList();

My result set could be big so I don’t want to load my entities for count query, so tell me efficient way to get total count like rowCount() method on Criteria (I think its there in Hibernate’s Criteria).

  • 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-20T18:14:48+00:00Added an answer on May 20, 2026 at 6:14 pm

    Thanks Vladimir!
    I took your idea and used separate count query to use my existing array of predicates in it. Final implementation looks like this:

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Brand> cQuery = builder.createQuery(Brand.class);
    Root<Brand> from = cQuery.from(Brand.class);
    CriteriaQuery<Brand> select = cQuery.select(from);
    .
    .
    //Created many predicates and added to **Predicate[] pArray**
    .
    .
    CriteriaQuery<Long> cq = builder.createQuery(Long.class);
    cq.select(builder.count(cq.from(Brand.class)));
    // Following line if commented causes [org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'generatedAlias1.enabled' [select count(generatedAlias0) from xxx.yyy.zzz.Brand as generatedAlias0 where ( generatedAlias1.enabled=:param0 ) and ( lower(generatedAlias1.description) like :param1 )]]
    em.createQuery(cq);
    cq.where(pArray);
    Long count = em.createQuery(cq).getSingleResult();
    .
    .
    select.where(pArray);
    .
    .
    // Added orderBy clause
    TypedQuery typedQuery = em.createQuery(select);
    typedQuery.setFirstResult(startIndex);
    typedQuery.setMaxResults(pageSize);
    List resultList = typedQuery.getResultList()
    

    Though this is working fine but still I am not sure why I have to write

    em.createQuery(cq);
    

    to get it working. Any Idea?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a search page that I'm implementing as part of an ASP.NET +
I'd like to create a web service that an application server can contact to
I'm implementing a search box using CodeIgniter, but I'm not sure about how I
I want to add the functionality to search for the nearest outlet(same like this
We are implementing a reporting system (.net 4.0, c#) that contains all the sender/recipient/timestamp
I'm Implementing a simple web app using Django, I want to get user's location
I'm implementing a long poll http connection using java servlet. How can I know
implementing publishActivity in PHP using the REST API using this code: $activity = array(
When implementing a needle search of a haystack in an object-oriented way, you essentially
While implementing a design using nested generic collections, I stumbled across those limitations apparently

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.