As JasperReport can have a JavaBean collection as a DataSource. Can we send a SetCollection with a single object which has references to multiple SetCollections. And use these references to pass to compiled jrxml file using mulitple calls to JasperFillManager.fillReport() each time passing a different SetCollection. just wanted to know if that compiled jrxml file will be filled with the last call or will have the data of each call to JasperFillManager.fillReport().
-
I've observed this problem of how to use multiple DataSources is faced my many. – Nilesh Jul 26 '12 at 15:08
2 Answers
This will be filled with the last one. You can't use multiple datasources that way. Although there're other ways to do that. Passing a Collection through the param Map is one of them.

- 1,620
- 2
- 21
- 28
-
Thanks and i agree about passing through param Map, but in jrxml design I'm not able to access the field of the class whose collection I'm passing in param Map. – Nilesh Jul 26 '12 at 07:32
-
Like Suppose Profile is my class and I'm passing the collection Set or ArrayList through param map. While accessing all i can get are the methods of collection and not the actual members of Profile class. And if i do something like $P{ProfileSet}.iterator().next().getName() it throws e like net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: 1. The method getFirstName() is undefined for the type Object value = ((java.util.Collection)parameter_PROFILE.getValue()).iterator().next().getFirstName(); //$JR_EXPR_ID=8$ – Nilesh Jul 26 '12 at 07:43
-
I've figured out a way around. I'm generating different pdfs for different datasources and merging them using JRPDFExporter. The Only problem I'm facing now is when JRPDFExporter joins two jasperprints its not removing the empty space. I mean the next JasperPrint objects PDF generation starts from new page leaving the previous page half-filled. – Nilesh Jul 26 '12 at 09:51
-
1With JasperReports you could use subreports inside your Main Report, I think it will solve your problems. Create a subreport for each collection you want and add them like datasources inside each one(not in java code, inside ireport). There's something about in those questions: http://stackoverflow.com/questions/11451493/not-able-to-use-subreports-in-jasperreports/11460622#11460622 , http://stackoverflow.com/questions/11517748/creating-passing-java-bean-datasource-in-jasperreport/11527711#11527711 – Diego Urenia Jul 26 '12 at 10:40
-
I appreciate the logic but in your first link where we have to pass subreport as parameter. We've to create a paramater in main report. But the parameter type? i mean Object is not in the drop down list. It shows it can either be collection/set/io.InputStream or other java primitives – Nilesh Jul 26 '12 at 12:29
-
Yes, you have to pass it as Object and then set the Class Type according what you are using. Then the Object will be cast for the right type for you. – Diego Urenia Jul 26 '12 at 20:03
Finally!!.. Achieved the task. Thanks to @Vycuss as I achieved it using subreports only.
mainreport = JasperCompileManager.compileReport(//path of mainreport.jrxml);
subreport = JasperCompileManager.compileReport(//path of subreport.jrxml);
Map<String, Object> params = new HashMap<String, Object>();
params.put("SUB_REPORT", subreport);
params.put("DATA_SOURCE", empService.getEmpProject());
jprint1 = JasperFillManager.fillReport(mainreport,params,new JRBeanCollectionDataSource(empService.getEmpBean()));
JasperExportManager.exportReportToPdfFile(jprint1, "C://Test.pdf");
Approach: After the above code in Activator.java as i was creating an OSGi bundle. Create two parameter's in MainReport.jrxml 1st being "SUB_REPORT" with no type and 2nd being "DATA_SOURCE" with java.util.collection type. Now in MainReport provide new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{DATA_SOURCE}) as the Data Source Expression for the subreport element placed in the detail band. Also make sure that subreport has only column header, detail and column footer band. Also the fields in the subreport are to be created for corresponding member's of the javabeans.

- 303
- 7
- 20
-
You're welcome. If my answer/comments have helped you, you can vote as the answer chosen by you. – Diego Urenia Jul 26 '12 at 20:07
-
@Nilesh: Did you face any issues with total page numbers? i'm using the same procedure but my total pages not updated since both are different reports. please let me know how you overcome it – TP_JAVA May 30 '13 at 02:05