Note that there are some explanatory texts on larger screens.

plurals
  1. POJava coding best-practices for reusing part of a query to count
    primarykey
    data
    text
    <p>The <a href="https://stackoverflow.com/questions/1600215/implementing-result-paging-in-hibernate-getting-total-number-of-rows">implementing-result-paging-in-hibernate-getting-total-number-of-rows</a> question trigger another question for me, about <strong>some implementation concern</strong>:</p> <p>Now you know you have to reuse part of the HQL query to do the count, how to reuse efficiently?</p> <p>The differences between the two HQL queries are:</p> <ol> <li>the selection is <code>count(?)</code>, instead of the pojo or property (or list of)</li> <li>the fetches should not happen, so some tables should not be joined</li> <li>the <code>order by</code> should disappear</li> </ol> <p>Is there other differences?</p> <p>Do you have <strong>coding best-practices to achieve this reuse efficiently</strong> (concerns: effort, clarity, performance)?</p> <p>Example for a simple HQL query:</p> <pre><code> 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 </code></pre> <hr> <p><strong>UPDATED</strong></p> <p>I received answers on:</p> <ul> <li>using <em>Criteria</em> (but we use HQL mostly)</li> <li><em>manipulating the String</em> query (but everybody agrees it seems complicated and not very safe)</li> <li><em>wrapping the query</em>, relying on database optimization (but there is a feeling that this is not safe)</li> </ul> <p>I was hoping someone would give options along another path, more related to String concatenation.<br> <strong>Could we build both HQL queries using common parts</strong>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload