• 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

Using Struts to iterate over queries with aggregates

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm working on an application where i need to process a hibernate query similar to the following:

Using this query, I hope to obtain a list of books with their various properties (title, author, number of pages, etc.).

my struts code inside of the view processes the list as follows:

What I want to add to this display is a final count of all the books returned by the query using:

How would I go about programming my struts code so that the count can be displayed? I realize that I can add a line such as:

But this value isn't accurate if I'm parsing out my pages with the following code before returning the list:


Thanks for any help...
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The <s:iterator> tag includes a status attribute. You can use this to get the count of rows provided by the iterator (among other things). For example:
 
Eugene Williams
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Mark, and for the suggestion. I apologize for not being more descriptive in my post. I am aware of this method for determining the number of items which were printed during the iteration process, however, I need to display the total number of books in the database rather than just the number of books within the display. Here is how the process works:

1. A user clicks a button to view a list of books
2. The application returns the first 25 books in the DB
3. The user clicks "Next" to review the next 25, and so on

What I need is a way to display the total number of books in the database on every page (i.e. "Viewing 1-25 of 472 books"). More specifically, I need to know how to handle a Hibernate query which requests data that is not native to the Object being queried; books have titles, authors, year published, and page numbers. Sometimes I want to count the total number of books matching a query:

or display the total number of pages in books covering a specific topic:

I'm not sure how to handle these aggregations in my Struts view during iteration.

Thanks.
 
Eugene Williams
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, after working on this today I was able to figure out how it should be done. I post this so that anyone who may be searching will know what steps to take.

The first thing I had to do was modify my query to include a handy subquery. Here's the new one:

The second part required changing the Struts view:

This way, not matter how many books I have displayed in my list (using paging methods), I can still display the total number of books matching the query. The key to figuring this out was understanding how the query was being processed and the list generated. My query results in multiple rows containing two columns (the first is the "Book" object, the second is the book count). Supplying an index to the OGNL expression gives me access to the appropriate column within the list.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That count won't show the total number of records returned by your query. It will return the total number of records in the table.
 
Eugene Williams
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. But, this is exactly what I needed. I know what is necessary to display the number of records in the current list, as mentioned in an earlier post. The issue I wanted to resolve was that where I wanted to display a count such as:

"Displaying Books 26 through 50 of 289"

The "289" number is what I was trying to retrieve (check out yesterday's - March 16th's - post at 9:07 A.M. for a better understanding of what I mean)
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should probably just let this go, as I don't want to argue with you if you're sure what you want

However, if you have 5000 books in your database, and your user searches for all Java books, of which there are 450. When you display these 450 books 25 at a time, you really want to see:

while on page 1: Displaying books 1 through 25 of 5000
while on page 2: Displaying books 26 through 50 of 5000
...
while on page 18: Displaying books 425 through 450 of 5000

?
 
Eugene Williams
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for pointing out the error in my query. Here's an updated version

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic