Sunday, August 28, 2011

Difference Between Fetched Row Count and Just Row Count

While implementing various validation rules in ADF BC, we can call two methods to get number of rows for specific entity - getFetchedRowCount() and getRowCount(). What's the difference between these two and what you should keep in mind? In case if Master-Detail screen is separated into two pages or two fragments - detail entity will be populated and data will be fetched only after this entity will be directly accessed. This means that getFetchedRowCount() returns count for rows already loaded into memory. In contrary, getRowCount() loads rows into memory (if there are no rows loaded) and then returns row count. Both of these methods are good and useful in specific use cases. ADF developer should understand the difference to avoid unexpected behavior.

Download sample application - This example implements simple Master-Detail relationship between Departments and Employees:

Master-Detail splits into two parts on UI as well, one page for Departments and another for Employees:

During our experiment, we will load only Master page with Departments data and invoke remove() operation. Inside remove() operation, we can implement validation rule to check if any child record exist for current Master record we are deleting. Let's say we are using getFetchedRowCount() method for this:

Keep in mind, Detail screen is not opened yet - only Master screen records are loaded into memory. Invoke remove() operation by clicking Delete button:

There are no records fetched yet for detail, so it looks like there are no Detail records for current Master:

Now we execute getRowCount(), this method will return real count - not from memory:

After getRowCount(), again is executed getFetchedRowCount() method.

We have learned - first there are no records loaded, we are executing getRowCount() it populates records into memory. After that, getFetchedRowCount() returns correct number of rows as well:

Hopefully this helps to understand better specific difference between two methods for row count.


Milkbird said...

Hello, according to API Reference:

Oracle Fusion Middleware Java API Reference for Oracle ADF Model :

public int getRowCount()
Counts the total number of rows in this row set.

This method retrieves all rows from the View Object by executing the View Object's query and then calling next() until the last row is retrieved. Thus, since it iterates through the View Object one record at a time, this method may be slow.

If you are working with a large number of rows, or if your application demands a fast response, use getEstimatedRowCount to obtain a quicker count.

Yet for validation that an accessor has any row the fastest way would be accessor.first()!=null


Andrejus Baranovskis said...

Yes of course, but it all depends on use case requirements. Sometimes developers need to compare against total number of rows and not to check just if at least one row exists.

This sample is created to highlight getFetchedRowCount method, to get number of rows you are free to use any of the methods.