[hibernate-dev] Query Extension: adding count method

Gaetano Perrone gperrone at email.it
Tue Oct 23 11:09:45 EDT 2007


Developping a pagination module for an open source hibernate-struts based project 
(http://smartweb.sourceforge.net)
i was envolved in the problem of how to extrapolate the results size of a given generic  query (sql query, hql query or criteria)
My first solution consisted into a size() call on the returned collection but this means to extract the collection from the db
for knowing the  result size. 
With hibernate 3 the solution for criteria was too easy and consists into add Projection on the given generic Criteria object
that otherwise of the session.iterate() solution doesn't need to extract any raws . The only result extracted is the Integer result of the count wrapped query.
Otherwise an analoughe solution for Hql/SQL Query is not very easy because of the protected visibility of the getNamedParams() method of org.hibernate.impl.AbstractQueryImpl
class. 
this deny any users to access to the query parameters needed to wrap and execute query.

To resolve this problem i tought a generich solution that improves query class adding a count() public method
that allows to ask for a query result preview count.

You can ask a given prepared query:
  - for uniqueresult: with the uniqueresult method
  - for raw list: with the list method
  - for raw count: with the count method   


My implementation class are attached.

I hope i could support the project with this little work...

Gaetano Perrone
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/hibernate-dev/attachments/20071023/c0366a23/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: hibernate-query-count.zip
Type: application/octet-stream
Size: 8069 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/hibernate-dev/attachments/20071023/c0366a23/attachment.obj 


More information about the hibernate-dev mailing list