Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get the number of rows?

 
Brian Grey
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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;
}
 
Loren Rosen
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic