JasperReports – Subreport, datasource and other stuff

Fun with JasperReports

Download sample

This page will show you how to succesfully create a report with a subreport, creating a data adapter attached to a bean factory, group a certain field, preview the report from the IDE and generate all the stuff as well as the pdf.

The sample were generated with Jasper Report library 6.0.3.

First of all, create a project named DataSourceSubReportSample, and create two models:

Company and Persons classes. The first one contains a list of Person objects.

MODEL

Company class

 Person class

FACTORY

Let’s create the factory class that will generate the entities:

TIBCO Jaspersoft Studio

DATA ADAPTERS

Under Data Adapter item of  Repository Explorer view create a new Data Adapter; Choose the Factory class and the method that will result the collection of the data bean to display.

Data Adapter

Create the main report and point the datasource to the data adapter created.

DATASET

It is possible to automatically generate report fields just with setting the data model pointed by the report.

See the image below

dataset

 SUBREPORT

When attaching a subreport to the master report, it’s important to set the Expression and the Data Source expression properties;

Expression property is used to find out which report to use, while the Data Source is the data attached on the subreport.

While Expression can be set to the relative path of the subreport .jasper (ex. “report/subreport.jasper”), it is also possible to load the compiled jasper from another source like an input stream.

For this purpose, create a parameter in the master report (in this case called PersonsSubReport) and set the Expression property to $P{PersonsSubReport}.

In this way it’s possible to inject the subreport from the code on the fill and build process. (We’ll see it later)

Set Data Source expression to:

The subreport element is now ready to works.

SubReport properties

GROUP FIELDS

In the PersonFactory class created above some persons have a common field value, the country property.

It is possible to display company’s person categorized by country, like the image below:

Group Band

To do so, choose Create Group on the subreport and choose the field wanted to group by – in the specific the country string – called Country.

In the Header section of the Country Group place the field country ($F{country}).

In the Footer section of the Country Group place the variable $V{Country_COUNT}.

To add the total count of the records on the Detail section of the report, use the predefinied variable $V{REPORT_COUNT}.

FILL AND BUILD

The code below fill, build, export to pdf and call the JasperViewer tool to see the result.

Notice that PersonsSubReport is the subreport object that is possible to get from a file or input stream object.

TEST

From the TIBCO Jaspersoft Studio take a look to the preview window to watch the result of out work;

if the data adapter is correctly set, the report will be correctly filled with the data expected.

Notice that in the subreport object, Expression property must be set to “reports/source/PersonSubReport.jasper” because parameter PersonsSubReport  is obviously empty at building time.

report preview

 Download sample

 

 

 

 

 

5 comments on “JasperReports – Subreport, datasource and other stuff

  1. Tavo

    Hi. First thank you for this info, for me was awasome.
    I need help because i downloaded the project but no info was displayed when executed. No errors in Jasper Studio 6.1.1 Final.

    Thank you!

  2. Shashank

    Hi,

    I downloaded the sample but it doesn’t say how you are sending the parameter “PersonsSubReport” to the report. Also the sub report mentions the “Person Adapter” as the default adapter at the top. I tried the sample code in my project and it is giving me “Error retrieving field from bean”

    1. Starred MediaSoft

      Hi Shashank,

      if you take a look on the FILL AND BUILD section on the end of the article you can see how PersonsSubReport is injected into the jasper source


      //Preparing parameters
      Map parameters = new HashMap();
      parameters.put("PersonsSubReport", subreport);

      // compile master report
      JasperCompileManager.compileReportToFile("reports/source/CompanyReport.jrxml", "reports/compiled/CompanyReport.jasper");

      // fill the report
      JasperFillManager.fillReportToFile("reports/compiled/CompanyReport.jasper", parameters, new JRBeanCollectionDataSource(CompanyFactory.getCompanies()));

Leave a Reply

Your email address will not be published. Required fields are marked *