Jaspersoft subreports let you include one report inside another, making it easier to present related information from multiple datasets in a single report. For example, a customer report can display customer details in the main report and corresponding order information in a subreport.
Creating effective Jaspersoft subreports involves more than inserting a report element. You need to configure the subreport design, data source, parameters, report expression, and layout properties correctly.
This guide explains how to create Jaspersoft subreports in Jaspersoft Studio, with practical examples, parameter-mapping instructions, configuration tips, and troubleshooting solutions.
A Jaspersoft subreport is an independent report template embedded within another report, commonly called the main report or master report.
The main report controls when the subreport runs, supplies the required data source or database connection, and passes parameters to it. The subreport generates its content, which the main report incorporates into the final output.
For example, imagine an organization generating a customer activity report:
This creates a master-detail report in which each customer's details appear alongside their corresponding order records.
Subreports are useful when a single report needs to combine different datasets or reusable report components.
Common use cases include:
| Use case | How a subreport helps |
|---|---|
| Master-detail reporting | Displays transaction details for each customer or account |
| Multiple datasets | Presents information retrieved through separate queries |
| Reusable report sections | Uses a common report layout across multiple parent reports |
| Financial reporting | Displays account summaries alongside transaction details |
| Operational reporting | Combines employee, department, and activity information |
Subreports can also be nested, allowing one subreport to contain another. However, design deeply nested or frequently executed subreports carefully because they can increase report-processing overhead.
Follow these steps to create a main report and embed a subreport using Jaspersoft Studio.
Start by creating the parent report that will contain the subreport.
For this example, assume the main report retrieves customer information from a database.
SELECT customer_id, customer_name, email
FROM customers;
The main report should include the customer fields that you want to display.
Important: Identify the field that will connect the main report to the subreport. In this example, that field is customer_id.
Create the subreport as a separate report template.
The subreport will retrieve order information associated with each customer.
For example, use the following SQL query:
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = $P{ParentCustomerID};
Here, ParentCustomerID is a parameter that will receive the customer ID from the main report.
Configure the subreport layout:
The subreport's dimensions should be planned around the parent report's available width and expected content.
Parameters allow the main report to pass values to the subreport.
In this example, the main report passes the customer ID so that the subreport retrieves only that customer's orders.
In the subreport:
Example parameter declaration:
<parameter name="ParentCustomerID"
class="java.lang.String"/>
The example assumes customer_id is stored as a string. If the database field is an integer, use the corresponding Java type, such as java.lang.Integer.
Important: The parameter name and data type must match the value being passed from the main report. Parameter names are case-sensitive.
After designing the subreport, compile it to generate the .jasper file.
The two file types serve different purposes:
| File type | Purpose |
|---|---|
| .jrxml | XML report design file that you can edit |
| .jasper | Compiled report object used when filling the report |
The parent report must be able to locate the compiled subreport at runtime. Keep the compiled file in the appropriate project or deployment location.
Now embed the compiled subreport in the parent report.
For example, the expression may reference the compiled report by name:
"CustomerOrders.jasper"
The exact expression depends on where the compiled report is stored and how your application loads report resources.
Tip: A relative path can be useful when the parent and subreport are deployed together. Confirm that the path resolves correctly in the actual runtime environment, not just in the Studio workspace.
This is one of the most important steps in creating a working subreport.
The subreport needs access to the appropriate data source and must receive the parameter values from the main report.
If the main report uses a JDBC connection and the subreport should use the same connection, configure the subreport's Connection Expression:
$P{REPORT_CONNECTION}
This passes the JDBC connection used by the main report to the subreport.
Alternatively, use a Data Source Expression when the subreport should receive a JRDataSource object.
These are alternative approaches. Do not configure both expressions for the same subreport element.
In the subreport properties:
Example:
$F{customer_id}
The mapping should look like this:
| Main report | Subreport |
|---|---|
| Field: customer_id | Parameter: ParentCustomerID |
| Expression: $F{customer_id} | Receives the customer ID |
| Data type: Must match the parameter | Data type: Must match the source value |
When the main report processes a customer record, the subreport receives that record's customer ID and uses it in its SQL query.
Correct layout configuration helps prevent blank spaces, overlapping elements, and incomplete content.
Review these important properties:
| Property | Purpose |
|---|---|
| Position Type: Float | Allows the subreport to move down when preceding elements expand |
| Stretch with Overflow / Overflow Type | Controls how the subreport accommodates content beyond its initial height; available options depend on the Studio version |
| Remove Line When Blank | Removes the element's allocated blank space when it does not print content |
| Connection Expression | Supplies the JDBC connection used to execute the subreport query |
| Data Source Expression | Supplies a JRDataSource object to the subreport |
Recommended configuration checks:
The available property names and options can vary between Jaspersoft Studio and JasperReports versions.
Once the subreport is configured, preview the main report to validate the complete output.
Check the following before deployment:
Consider a customer reporting application that needs to display customer information and related order details.
SELECT customer_id, customer_name, email
FROM customers;
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = $P{ParentCustomerID};
Subreport parameter:
<parameter name="ParentCustomerID"
class="java.lang.String"/>
Main report parameter mapping:
$F{customer_id}
Connection expression:
$P{REPORT_CONNECTION}
The report execution follows this sequence:
This approach produces a consolidated customer report with related order information.
Even when the subreport is correctly designed, configuration issues can affect the final output.
| Problem | Possible cause | What to check |
|---|---|---|
| Subreport is not displaying | Incorrect report expression or file path | Verify the compiled .jasper file location and expression |
| Subreport displays no records | Incorrect parameter mapping | Check parameter names, values, and data types |
| Blank space appears | Empty subreport or unused layout space | Review Remove Line When Blank and band settings |
| Subreport overlaps other elements | Incorrect positioning or sizing | Review Position Type and element dimensions |
| Compilation fails | Invalid expressions or missing dependencies | Check compilation errors and required libraries |
| Database connection fails | Incorrect connection or data-source configuration | Verify connection settings, permissions, and the selected expression |
Troubleshooting tip: Test the subreport independently before embedding it in the main report. This helps determine whether the problem is in the subreport's query and design or in the parent report's configuration.
Beyond displaying related records, JasperReports supports additional subreport functionality.
A subreport can return calculated values to the parent report through return-value configuration.
For example, a subreport can calculate the total order amount for a customer and pass that value to a variable in the main report.
The return-value configuration identifies:
A subreport can contain another subreport. This is useful when a report requires multiple levels of related information.
For example:
Use nested structures selectively because they can increase processing complexity.
A separately designed subreport can be reused in multiple parent reports. This helps maintain consistent layouts and reduces the need to recreate the same report section.
Follow these practices to make subreports easier to maintain and troubleshoot.
Creating Jaspersoft subreports involves designing separate report templates, configuring data sources, passing parameters, compiling the subreport, and embedding it within the main report.
By following these steps and validating the report output, you can build structured reports that combine related information while keeping individual report components reusable and manageable.
For organizations managing large reporting environments, maintaining and modernizing complex report assets can also become an important consideration.
Explore DataTerrain's BI Reports Conversion Services to learn about approaches to transitioning reporting assets between BI platforms.
Need help assessing your BI reporting environment? Contact DataTerrain to discuss your reporting and migration requirements.
Explore these DataTerrain resources for more information about Jaspersoft, BI reporting, and report migration:
For additional guidance, refer to the official JasperReports Subreport Sample and Jaspersoft Studio Subreports Documentation.