---
name: subreports-datasets
description: Wire JasperReports subreports and component dataset runs, including subreport resource lookup in packaged applications, passed parameters, per-record data sources or connections, sub-datasets and returned values. Use when a subreport is not found, child data is empty, repeated or mixed between records, or totals from a subreport are wrong or missing; not for a standalone report without subreports or dataset runs.
---
# Wire subreports and dataset runs
## Establish the composition contract
Identify the exact engine/Java versions, how the application is packaged and deployed, where compiled `.jasper` or source `.jrxml` files live, which data each child needs (a JDBC query, a collection field or another source), the parameters each child receives and which values flow back to the parent. Ask for the packaging and data contract instead of guessing paths or reusing one data object for every child.
## Workflow
1. Resolve subreports through the classpath or a compiled object, not through file system paths. Use a classpath-relative name such as `reports/order-lines.jasper`, or pass a compiled `JasperReport` in a parameter of class `java.lang.Object` or `net.sf.jasperreports.engine.JasperReport`. Do not point the expression at `src/main/resources/...` or another build-time path. When the reports are in a JAR that the default class loader cannot see, pass that loader in `REPORT_CLASS_LOADER`. The tested 7.1.1 runtime then finds the packaged `.jasper`; without it, the fill fails with `Resource not found at: reports/order-lines.jasper.`, so treat that error as a lookup or packaging problem.
2. Ship compiled subreports, or compile each `.jrxml` once at startup and cache the result. Compiling a subreport for every parent record wastes time and hides compile errors until fill time.
3. Give every child its own cursor. A JRDataSource is consumed as it is read. Create a new one per parent record, for example `new JRBeanCollectionDataSource($F{lines})` or `new JRMapCollectionDataSource($F{lines})`, rather than passing one data source parameter to every subreport or dataset run. In the tested runtime, a shared data source is consumed by the first child and every later child prints nothing. For JDBC children, pass `$P{REPORT_CONNECTION}` as the connection expression and let each child run its own parameterized query.
4. Pass parameters explicitly. A subreport or dataset run sees only the parameters passed to it, plus a parameters map when one is given. Declare each parameter in the child with the same name and class, and bind query values with `$P{}` rather than `$P!{}`.
5. Return values deliberately. Use `<returnValue subreportVariable="..." toVariable="..."/>` on the subreport and declare the target variable in the parent, usually with `calculation="System"`; add `calculation="Sum"` (or another calculation) to combine values across records. A text field in the same band as the subreport needs `evaluationTime="Band"` to show the returned value; evaluated `Now`, it shows the previous record's value or null. A child with no rows returns a null sum, so guard the printed value.
6. Use a dataset run (`datasetRun` with a `subDataset` in a list, table or chart) when the child is a simple repeated block that belongs to the parent layout, and a subreport when the child needs its own bands, groups or pagination. Both follow the same cursor and parameter rules.
## Validate and deliver
Fill with at least two parent records that have different child data, one parent without child rows and a missing-subreport case. Assert each child's printed values, returned totals, the explicit missing-resource failure and that later records are not empty. Package the reports as the deployed application does (for example, build a test JAR) and test the lookup from that package. State commands and results, and label any packaging or data path you could not execute as unverified.
## Sources
- [Sample reference](https://jasperreports.sourceforge.net/sample.reference/README.html)
- [Schema reference](https://jasperreports.sourceforge.net/schema.reference.html)
- [API](https://jasperreports.sourceforge.net/api/index.html)
— ‹guest · 2026-11-29› —
این نسخه با نشانهگذاری اختصاصی حساب شما نمایش داده میشود. برای نصب سریع، به تب نصب و راهاندازی بروید.