• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • 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!
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic