title: How to pass master report data source to subreport (JasperReports)?
body: <p>I am trying to generate a JasperReport PDF with subreports but I can't access the JRBeanCollectionDataSource (coming from a List) passed as parameter to the main report. Or rather, I just get a blank subreport. I expect to be looping of some sort since I am passing a JRBeanCollectionDataSource.</p>
  
  <p>This is my current code:</p>
  
  <p>Java code:</p>
  
  <pre><code>@PostMapping(path = "/generate-contract", consumes = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity &lt; byte[] &gt; report(
      @RequestBody Contract contract) {
  
      Map&lt;String, Object&gt; params = new HashMap&lt;&gt;();
  
      // add params to the MASTER REPORT
      params.put("farmName", contract.getFarmName());
      params.put("farmLogo", contract.getFarmLogo());
  
      // create a JRBeanCollectionDataSource of the List&lt;Signatory&gt; list
      JRBeanCollectionDataSource signatoriesListSubreportDataSource = new JRBeanCollectionDataSource(contract.getSignatoriesList());
  
      // add it as parameters to be read by the master report
      params.put("signatoriesListSubreportDataSource", signatoriesListSubreportDataSource);
  
      byte[] bytes = reportService.generatePDFReport("my-master-report", params);
  
      return ResponseEntity
          .ok()
          .header("Content-Type", "application/pdf; charset=UTF-8")
          .header("Content-Disposition", "inline; filename=\"" + contract.getFarmName() + ".pdf\"")
          .body(bytes);
  }
  </code></pre>
  
  <p>I am calling a .generatePDF() method where it actually compiles the master report and the subreport as well. I don't know if this is accurate though</p>
  
  <pre><code>@Override
      public byte[] generatePDFReport(String inputFileName, Map&lt;String, Object&gt; params) {
          return generatePDFReport(inputFileName, params, new JREmptyDataSource());
      } // this is where it first goes through
  
  @Override
  public byte[] generatePDFReport(String inputFileName,
      Map &lt;String, Object&gt; params,
      JRDataSource dataSource) { // then proceeds to this method, creating a blank dataSource
  
      byte[] bytes = null;
      JasperReport jasperReport = null;
      try (ByteArrayOutputStream byteArray = new ByteArrayOutputStream()) {
          // Check if a compiled report exists
          if (storageService.jasperFileExists(inputFileName)) {
              jasperReport = (JasperReport) JRLoader
                  .loadObject(storageService.loadJasperFile(inputFileName));
          }
  
          String mySignatoriesSubReportFileName = "my-subreport-file-name-here";
  
          // Compile report from source and save
          String jrxml = storageService.loadJrxmlFile(inputFileName);
          String mySubReport = storageService.loadJrxmlFile(mySignatoriesSubReportFileName);
  
          // add logs of master report + subreport here
          log.info("{} loaded. Compiling report", jrxml);
          log.info("{} loaded. Compiling report", paiwiSignatoriesSubReport);
  
          params.put("SUBREPORT_DIR", FileSystemUtil.rootLocation.toString() + "\\");
  
          // compile the master report together with the subreports
          jasperReport = JasperCompileManager.compileReport(jrxml); // master report
          JasperReport compiledMySubReport = JasperCompileManager.compileReport(mySubReport); // sub-report
  
          // Save compiled report. Compiled report is loaded next time
          JRSaver.saveObject(jasperReport,
              storageService.loadJasperFile(inputFileName));
  
          JRSaver.saveObject(compiledMySubReport,
              storageService.loadJasperFile(mySignatoriesSubReportFileName));
  
          JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params,
              dataSource);
          bytes = JasperExportManager.exportReportToPdf(jasperPrint);
      } catch (JRException | IOException e) {
          e.printStackTrace();
          log.error("Encountered error when loading jasper file", e);
      }
  
      return bytes;
  }
  </code></pre>
  
  <p>This is what my master-report.jrxml looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Autogen-contract" pageWidth="612" pageHeight="1008" columnWidth="540" leftMargin="36" rightMargin="36" topMargin="36" bottomMargin="36" whenResourceMissingType="Error" uuid="4d5cd764-f78c-4d28-b2cc-4dadc9e584a1"&gt;
  
      &lt;!-- Location of the compiled subreport. --&gt;
      &lt;parameter name="SUBREPORT_DIR" class="java.lang.String"/&gt;
  
      &lt;!-- For data source of sub-reports --&gt;
      &lt;parameter name="mySignatoriesListSubReportDataSource" isForPrompting="true" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/&gt;
  
      &lt;!-- This one works as of the master report --&gt;
      &lt;parameter name="farmName" class="java.lang.String"/&gt;
  
      &lt;!-- More details of the master report here.. --&gt;
  
      &lt;textField&gt;
          &lt;reportElement key="" x="135" y="120" width="270" height="20" isRemoveLineWhenBlank="true" uuid="2974af73-36ae-4781-a7b1-1b0a4a498c5c"/&gt;
          &lt;textElement textAlignment="Center" verticalAlignment="Middle"&gt;
              &lt;font fontName="Times New Roman" size="12" isBold="true"/&gt;
          &lt;/textElement&gt;
          &lt;textFieldExpression&gt;&lt;![CDATA[java.lang.String.format("%s", $P{farmName})]]&gt;&lt;/textFieldExpression&gt;
      &lt;/textField&gt;
  
      &lt;subreport&gt;
          &lt;reportElement x="1" y="520" width="537" height="20" uuid="fcf375d6-3935-42c9-bc81-1448e6f03ebf"/&gt;
          &lt;dataSourceExpression&gt;&lt;![CDATA[$P{mySignatoriesListSubReportDataSource}]]&gt;&lt;/dataSourceExpression&gt;
          &lt;subreportExpression class="java.lang.String"&gt;&lt;![CDATA[$P{SUBREPORT_DIR} + "my-subreport.jasper"]]&gt;&lt;/subreportExpression&gt;
      &lt;/subreport&gt;
  
      &lt;!-- More details of the master report here.. --&gt;
  
  &lt;/jasperReport&gt;
  </code></pre>
  
  <p>This is what my-subreport.jrxml looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!-- Created with Jaspersoft Studio version 6.5.1.final using JasperReports Library version 6.5.1  --&gt;
  &lt;jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="paiwi-signatories-subreport" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="55585643-052e-4eee-9632-3c9b06dbec8f"&gt;
      &lt;subDataset name="Dataset1" uuid="01eaeaca-361c-429f-9e71-e974b4f2beda"&gt;
          &lt;queryString&gt;
              &lt;![CDATA[]]&gt;
          &lt;/queryString&gt;
      &lt;/subDataset&gt;
  
      &lt;queryString&gt;
          &lt;![CDATA[]]&gt;
      &lt;/queryString&gt;
  
      &lt;!-- I have a name and address in the Signatory object I passed as a list of Data Source.. --&gt;
      &lt;!-- But this doesn't work.. or atleast it doesn't iterate anything --&gt;
      &lt;field name = "name" class = "java.lang.String"/&gt;
      &lt;field name = "address" class = "java.lang.String"/&gt;
  
      &lt;detail&gt;
          &lt;band height="24" splitType="Stretch"&gt;
              &lt;textField&gt;
                  &lt;reportElement x="0" y="0" width="140" height="20" uuid="5f357ce5-9928-4657-98f7-c7eace9b9e84"/&gt;
                  &lt;textElement verticalAlignment="Middle"&gt;
                      &lt;font fontName="Times New Roman" size="12"/&gt;
                  &lt;/textElement&gt;
                  &lt;textFieldExpression&gt;&lt;![CDATA[$F{name}]]&gt;&lt;/textFieldExpression&gt;
              &lt;/textField&gt;
              &lt;textField&gt;
                  &lt;reportElement x="140" y="0" width="400" height="20" uuid="0390ae27-e46b-4bb4-87d0-83d59abfc22e"/&gt;
                  &lt;textElement verticalAlignment="Middle"&gt;
                      &lt;font fontName="Times New Roman" size="12"/&gt;
                  &lt;/textElement&gt;
                  &lt;textFieldExpression&gt;&lt;![CDATA[$F{address}]]&gt;&lt;/textFieldExpression&gt;
              &lt;/textField&gt;
          &lt;/band&gt;
      &lt;/detail&gt;
  &lt;/jasperReport&gt;
  </code></pre>
  
  <p>I can't access the fields in the sub-report, I don't even know if the parameter <code>farmSignatoriesListSubReportDataSource</code> in the master-report.xml actually goes through and enters the subreport as a valid data source. </p>