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 417411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:34:39+00:00 2026-05-12T18:34:39+00:00

The implementing-result-paging-in-hibernate-getting-total-number-of-rows question trigger another question for me, about some implementation concern : Now

  • 0

The implementing-result-paging-in-hibernate-getting-total-number-of-rows question trigger another question for me, about some implementation concern:

Now you know you have to reuse part of the HQL query to do the count, how to reuse efficiently?

The differences between the two HQL queries are:

  1. the selection is count(?), instead of the pojo or property (or list of)
  2. the fetches should not happen, so some tables should not be joined
  3. the order by should disappear

Is there other differences?

Do you have coding best-practices to achieve this reuse efficiently (concerns: effort, clarity, performance)?

Example for a simple HQL query:

    select       a     from A a join fetch a.b b where a.id=66 order by a.name
    select count(a.id) from A a                  where a.id=66

UPDATED

I received answers on:

  • using Criteria (but we use HQL mostly)
  • manipulating the String query (but everybody agrees it seems complicated and not very safe)
  • wrapping the query, relying on database optimization (but there is a feeling that this is not safe)

I was hoping someone would give options along another path, more related to String concatenation.
Could we build both HQL queries using common parts?

  • 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-12T18:34:39+00:00Added an answer on May 12, 2026 at 6:34 pm

    Nice question. Here’s what I’ve done in the past (many things you’ve mentioned already):

    1. Check whether SELECT clause is present.
      1. If it’s not, add select count(*)
      2. Otherwise check whether it has DISTINCT or aggregate functions in it. If you’re using ANTLR to parse your query, it’s possible to work around those but it’s quite involved. You’re likely better off just wrapping the whole thing with select count(*) from ().
    2. Remove fetch all properties
    3. Remove fetch from joins if you’re parsing HQL as string. If you’re truly parsing the query with ANTLR you can remove left join entirely; it’s rather messy to check all possible references.
    4. Remove order by
    5. Depending on what you’ve done in 1.2 you’ll need to remove / adjust group by / having.

    The above applies to HQL, naturally. For Criteria queries you’re quite limited with what you can do because it doesn’t lend itself to manipulation easily. If you’re using some sort of a wrapper layer on top of Criteria, you will end up with equivalent of (limited) subset of ANTLR parsing results and could apply most of the above in that case.

    Since you’d normally hold on to offset of your current page and the total count, I usually run the actual query with given limit / offset first and only run the count(*) query if number of results returns is more or equal to limit AND offset is zero (in all other cases I’ve either run the count(*) before or I’ve got all the results back anyway). This is an optimistic approach with regards to concurrent modifications, of course.

    Update (on hand-assembling HQL)

    I don’t particularly like that approach. When mapped as named query, HQL has the advantage of build-time error checking (well, run-time technically, because SessionFactory has to be built although that’s usually done during integration testing anyway). When generated at runtime it fails at runtime 🙂 Doing performance optimizations isn’t exactly easy either.

    Same reasoning applies to Criteria, of course, but it’s a bit harder to screw up due to well-defined API as opposed to string concatenation. Building two HQL queries in parallel (paged one and “global count” one) also leads to code duplication (and potentially more bugs) or forces you to write some kind of wrapper layer on top to do it for you. Both ways are far from ideal. And if you need to do this from client code (as in over API), the problem gets even worse.

    I’ve actually pondered quite a bit on this issue. Search API from Hibernate-Generic-DAO seems like a reasonable compromise; there are more details in my answer to the above linked question.

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

Sidebar

Related Questions

I am implementing a search module with result page support paging. The example provided
I am implementing synronous class in back ground process I am getting some results.
I am implementing a complex search module with result page support paging. Most of
I'm implementing a TryParse(string s, Out object result) method. If the parse fails, I
Implementing Equals() for reference types is harder than it seems. My current canonical implementation
Implementing the ScriptControlClass was extremely easy, unfortunately the side effects with the language implementation
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
I am implementing a jquery autocomplete on a search form and am getting the
I am implementing a UIlabel that displays the result of a user's button clicks
While reading about and implementing FRP I am confused by the semantics of the

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.