JRXML reports use a query to select the data to be returned from the data source. The query can be defined in the JRXML itself, or it can be saved in the repository. A query in the repository can be re-used by multiple JasperReports. See the sample queries in the /SuperMart Demo/Common folder in the repository.

Reusing a query enables you to adapt reports to different audiences. The query returns the same data from the same data source every time, but each report presents the data in a different way. Reusing a query simplifies maintenance of reports, as well, since all the reports return the same data. Also, separating the query from the JRXML makes it easier to maintain large numbers of reports when the data source changes and the query needs to be updated.

See the JasperReports Server User Guide for complete instructions on using JRXML reports. For another means of adapting reports to different audiences, refer to the chapter on Domains in that manual.

Query resources can also be used to populate list input controls. For more information, see the chapter on cascading input controls in the JasperReports Server Ultimate Guide.

To create a reusable query resource:

1. Login as an administrator.
2. Click View > Repository and expand the folder tree to locate the folder in which you want to create the query. If you installed the sample data, the suggested folder is Input Data Types.
3. Right-click the folder and select Add Resource > Query from the context menu.

The Add Query page appears.

Add Query - Name the Query Page

4. Enter a name and optional description for the query and click Next. The resource ID is filled in automatically.

The Link a Data Source page appears.

Add Query - Link a Data Source Page

5. Select the data source and click Next. For this example, leave the default, but other options are presented:
     Do not link a data source. If no data source is associated with the query, the server uses the data source that is associated with the report that references this query.
     Create a new data source. You can define a local data source within this query resource that is not accessible to any other resource. This new data source overrides any data source specified in reports that use this query.
     Select data source from repository. This creates a reference to a data source in the repository. The data source you select overrides any data source specified in reports that use this query.

After clicking Next, the Define the Query page appears.

Add Query - Define the Query Page

6. Select SQL as the Query Language.

The query language Domain (sl) is selected when opening Domain-based queries created in versions of the server prior to 3.7. It is used only for backward compatibility and should not be selected for new Domain-based queries.

7. Enter the following test in the Query String field:
SELECT * FROM orders
8. Click Save.

By default, JasperReports Server supports SQL, HQL (Hibernate), HiveQL (Hadoop-Hive), MongoDB, and Domain queries, while JasperReports supports several more (such as EJBQL, xPath and MDX). However, JasperReports Server can support queries in additional query languages if there is a properly-configured query executor implementation for each additional language when the server is deployed.

A specialized bean data source can be used to support multiple query languages. For information about bean data sources, refer to section Bean Data Sources. Another option is to add new types of data sources to the server, thus extending the reach of the JasperReports Server platform by leveraging one of its main extension points. Custom data sources are described in the JasperReports Server Ultimate Guide.