HowTo fetch aggregrate data using ReportQuery in OracleAS TopLink 10g

OracleAS TopLink - HowTo fetch aggregate data using ReportQuery

Date: 13/Apr/2004

After completing this HowTo you should be able to:
• Understand how to fetch aggregate data using ReportQuery in OracleAS TopLink 10g
• Run the sample using the instruction given

Table of Contents

Introduction
Prerequisites
Software Requirements
HowTo fetch aggregate data using ReportQuery in OracleAS TopLink
Useful References

Introduction 

Oracle Application Server TopLink is an advanced object-to-relational persistence. It helps to build high performance applications that store persistent data in a relational database. OracleAS TopLink query framework API provide report querying functionalites which uses reporting functionalities and features provided by the database to retrieve aggregate data.

Prerequisites 

Before we get started, the following assumptions are made in this document:
  • Minimum knowledge about TopLink.
  • Must have HR schema installed on the database to run the sample

Software Requirements 

HowTo fetch aggregate data using ReportQuery in OracleAS TopLink 

Report Query enables you to retrieve aggregate data from a set of objects and their related objects. Report Query allows you to use expression for grouping data, specifying selection criteria and ordering criteria. It allows to use database aggregation function like MIN, MAX, SUM, AVG, and COUNT. Report Query returns results as collection of ReportQueryResult objects, which is similar to DatabaseRow or Map. The following code snippet shows how to fetch aggregate data using ReportQuery

    ExpressionBuilder emp = new ExpressionBuilder();

// Construct Report Query
ReportQuery query = new ReportQuery(emp);
query.setReferenceClass(Employees.class);

// Add departmentId to the querys result
query.addAttribute("departmentId", emp.get("departmentId"));

// Add Count, MIN, MAX, AVG, SUM Aggregate functions to query result
query.addCount();
query.addMinimum("min-salary",emp.get("salary"));
query.addMaximum("max-salary", emp.get("salary"));
query.addAverage("average-salary", emp.get("salary").);
query.addSum("sum-salary",emp.get("salary"));

// Construct Date object for specifying criteria
Calendar fromDate = new GregorianCalendar(1900,0,1);
Date from = new Date(fromDate.getTime().getTime());
// Set Selection Criteria
query.setSelectionCriteria(emp.get("hireDate").roundDate("year").greaterThan(from));

// Set Order by clause
query.addOrdering(emp.get("departmentId"));

// Set Group by clause
query.addGrouping(emp.get("departmentId"));

// Execute the report query
Vector reports = (Vector) session.executeQuery(query);

Steps to run the sample 

  • Download the jar file from here and extract it.
  • Edit <SAMPLE_HOME>/src/project.xml, set database parameters in the tag connection-url.
  • Add toplink.jar, xmlparserv2.jar, connector.jar, classes12.jar and <SAMPLE_HOME>/src to the classpath
  • From <SAMPLE_HOME>/src directory, compile and run java oracle.otnsamples.toplink.reportquery.ReportQuerySample

Useful References 

Please enter your comments about this sample in the OTN Sample Code Discussion Forum.


How To fetch aggregate data using ReportQuery

Please rate this how-to:
Excellent
Good
Average
Below Average
Poor

 



E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy