Wednesday, October 18, 2006

BC4J+OA Framework:Get number of rows in a ViewObject without fetching all rows to middletier

In BC4J(Business Component for Java),Lets assume, we have a View Object(VO) based on a SQL.
The code below prints i as "0" i.e no rows are fetched to the middle tier.

vo.setMaxFetchSize(-1);
vo.executeQuery();
int i = vo.getFetchedRowCount();
System.out.println("i="+i);

Now use
getRowCount() instead of getFetchedRowCount();
and it would print correct no of records.

Explanation:
------------
executeQuery() doesn't actually fetch the first batch of rows right then and there. The next operation that requires a query (such as first(), for example) will do this. getRowCount() forces the last() row to be retrieved. But getRowCount() might be a costly affair in cases where we dont want the rows to be fetched from the DB, say we just want the count.

using getEstimatedRowCount() if you want the count without fetching all the rows, solves the same purpose.



No comments: