• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to limit resultset size through hibernate.cfg.xml?

 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of restricting the resultset rows per query, I would like to do it across the application. Is it possible to do so at the hibernate.cfg.xml file? Couldn't find any Hibernate properties to use for it.

Or, is there any JDBC property that I could use to limit the maximum rows returned?

Cheers!
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, please tell my you're using the Ganng of Four's (GOF's) Data Access Object (DAO) pattern. You are right?

I always like to implement finder methods in an abstract DAO class, that implements a GenericDAO interface, which provides a way to limit the resultset. As you can see, I'm a big fan of the Hibernate Criteria API:



Implementing DAO subclasses can then decide how to page through the results for the client. At the very least, subclasses can implement zero-arg findAll methods that will stop a resultset that is so large that it will destroy the hibernate cache and bring down the database.

I discuss the DAO pattern, and how to implement an elegant DAO implementation with Hibernate and JPA annotations on my site. You should check it out:

Simple Data Access Objects with Hibernate & JPA

Advanced DAOs with Java 5 Generics
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps I wasn't be clear. I do know to achieve it programmatically, but if I need to change the maximum rows to be returned by the query, I would have to do a code change.

I was thinking whether does the configuration file allows me to set it declaratively instead.

And yes, I'm using the Generic DAO pattern in Hibernate. I'm able to set the size at my generic DAO; but wish to avoid changing codes if I need to change the size (say from restricting it to max 1000 to max 500).

Cheers!
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hate to keep answering everything but your question, but I think that's exactly what I'm going to do...

I don't know of any way to set this in the deployment descriptor. But there are lots of things I don't know, so that's not definitive.

But I would say that perhaps the same results could be achieved by creating your own xml or text file, right there next to the config file, that you can read from your program. It could contain the max size of a result, and it could be edited easily without touching the code.

Just an idea. As my friends at PETA say, there's always more than one way to skin a cat.

-Cameron McKenzie
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for at least replying to my post.

What we did was to do it programmatically using the Criteria class. A convenience method (in the Generic DAO) was provided to set this size, which is actually calling Criteria.setMaxResults.

The max limit of the ResultSet is then stored in the database. So if there's a need to limit the size, a query is done to get this value from the database, then passed to the Generic DAO.

Was initially hoping to find some easy way out, but guess it is not so possible, at least I can't find the way to.

Thanks!
reply
    Bookmark Topic Watch Topic
  • New Topic