1

Here is my java objects :

Class1.java :

public class Class1 {
    public Class2 object2;
    ...
}

Class2.java :

public class Class2 {
    public Class3 object3;
    ...
}

Class3.java :

public class Class3 {
    public List<Class4> list4;
    ...
}

Class4.java :

public class Class4 {
    public String string1;
    public String string2;
    public String string3;

    public Class5 object5;
    ...
}

I have a report with a subreport and both use the same Datasource as you can see below :

public static void execute(List<Class1> data) throws FileNotFoundException, JRException {

BufferedInputStream reportStream = new BufferedInputStream(new FileInputStream("C:/Users/user1/Downloads/report2.jrxml"));

// Bing the datasource with the collection
JRDataSource datasource = new JRBeanCollectionDataSource(data, false);

Map<String, Object> parameters = new HashMap<String, Object>();

parameters.put("SUBREPORT_DIR", "C:\\Users\\user1\\Downloads\\");

parameters.put("SUB_DATA_SOURCE", data);

// Compile and print the jasper report
JasperReport report = JasperCompileManager.compileReport(reportStream);
JasperPrint print = JasperFillManager.fillReport(report, parameters, datasource);

// Export report to PDF
JasperExportManager.exportReportToPdfFile(print, "C:/Users/user1/Downloads/JavaBeansPDF.pdf");
}

SUB_DATA_SOURCE is a parameter that I defined into my report & my subreport in iReport with Parameter Class : java.util.List

I have tried to display simple values from Class1 into the report, and it works.

But my issue is how to display the content of my list List<Class4> list4 into the subreport.

I need to display this "table" in my report :


string1 ------- string2 ------- string3 ------- object5.getElement1 ...

abc ------------- cde ---------- test ------------- value1 ...


I don't know if my issue is clear, please tell if something is confusing or missing.

PS: I'm using JasperReports v5.6.0 and iReport v5.6.0

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Sinda MOKADDEM
  • 796
  • 2
  • 12
  • 35
  • I can't really understand why you are passing the List as SUB_DATA_SOURCE for the subreport, since the datasource will change for every Class1 in your List. I have posted a general answer ignoring the SUB_DATA_SOURCE parameter. – Petter Friberg May 02 '16 at 20:43
  • @PetterFriberg I'm passing SUB_DATA_SOURCE to be able to use its content into my subreport. That's the only why I found to do so. But maybe I have to specify that in my case I have always one item into `List data` – Sinda MOKADDEM May 03 '16 at 09:07
  • Check the answer and tell me what is not clear (no need to pass the parameter) – Petter Friberg May 03 '16 at 09:10

1 Answers1

0

You are passing List<Class1> as datasource this implies that you have this field definition in the report

<field name="object2" class="your.package.Class2"/>

To display the data in Class4, you need either to create a subreport for this data or use the <jr:table/> component.

The datasource for this subreport or <jr:table/> would be:

<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{object2}.getObject3().getList4())]]></dataSourceExpression>

Handling NullPointer is beyond this answer and I'm assuming you have public getters for all the fields

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109