• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Get the number of rows?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I get the number of rows on a specific table? I'm using Kodo JDO 3.1.5 with Oracle.


I want to know how many rows there are in the table before to execute the final query. I want to do this because when there are 100.000 rows or more I get a java.lang.OutOfMemoryError. So I don't to want to execute the final query if there are too many rows.

protected Collection execute(Class type, String filter) {

final Extent extent = getPersistenceManager().getExtent(type, true);
query = pm.newQuery(extent, filter);
query.setOrdering(orderBy + " " +
(orderAscending ? "ascending" : "decending"));

// TODO
// Get the number of rows
numberOfRows = Xxxxxxxxxxxxx;
if (numberOfRows > MAX_PER_COLLECTION) {
throw new XxxxxxException("Too Many Rows: " + result.size());
}
// TODO

final Collection result = (Collection) query.execute();

if (log.isDebugEnabled()) {
log.debug("Found " + result.size() + " match(es)");
}
return result;
}
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't supported in JDO1, but it looks like Kodo offers it as an extension: http://solarmetric.com/Software/Documentation/3.1.5/docs/ref_guide_aggregates_and_projections.html#ref_guide_aggregates_count

Simplifying their example, we have:

// determine the total employees
KodoQuery q = (KodoQuery) pm.newQuery (Employee.class);
query.setResult ("count(this)");
long employees = ((Long) q.execute ()).longValue ();

However, it's worth asking whether you really need to bring 100,000 row over from the database. Very often this can be avoided.
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic