Jump to content
Changes to the Jaspersoft community edition download ×

passing datalist to master and then subreport

2004 IR Help

Recommended Posts

By: neqo - ntapan

passing datalist to master and then subreport

2005-12-07 06:59


I have created a master report and I pass the data to the report with HibernateQueryResultDataSource (that implements JRDataSource), it passes the list from java code to the report.let say I have 3 fields in the masterreport. here how I produce the list :

List lstResult=new LinkedList();

Object[] obj = new Object[3];

List lstStudents = getStudents();

for(all students){







HibernateQueryResultDataSource ds = new HibernateQueryResultDataSource(lstResult, fields);

JasperPrint jasperPrint = JasperFillManager.fillReport(template, reportParameters,ds);


at that time, how can I pass list of data to the subreport, how can I get connect the master and subreport, what should be the subreportparameterexpression, connection and datasource expressions ???


can I take the list of data, that will be sent to subreport, to the masterreport as the field of masterreport ?


did anyone do this before ?

thanks for help...





By: macoute3 - macoute3

RE: passing datalist to master and then subreport

2005-12-07 07:31



I have the same problem, and resolve it.

The difference between SQL and HQL is not in: how to present data but how to execute the query.


The initial query (in the master report) just extract data for the master, and only for it.

Because, the sub query could validated only at runtime.


For example:


In master template:

select cpy.name

from Company cpy


In sub report:

select cpy.country

from Company cpy

where cpy.name = '$P{CompanyName}'


the master template defines a parameter:

CompanyName = $F{cpy.name}


So, in order to create the data source for the subreport, I use

the following class:

public class DataSourceProvider {

static public JRDataSource(String template, ReportParameter [] parameters) {

JasperReport jasperReport = loadTemplate(template);

// Get HQL query

String hqlQuery = jasperReport.getQuery().getText();

for(int i=0; i<parameters.length; i++) {

ReportParameter parameter = parameters;


String name = parameter.getName();

Object value = parameter.getValue();


hqlQuery = hqlQuery.replaceAll("[$][P][{]" + name + "[}]", value.toString());

hqlQuery = hqlQuery.replaceAll("[$][P][!][{]" + name + "[}]", value.toString());


// Query execution

List result = HQLQueryExecutor.execute(hqlQuery);


// Data Source

JRField [] jrFields = jasperReport.getFields();

JRDataSource jrDataSource = new HQLResultDataSource(result, jrFields);


return jrDataSource;






public class ReportParameter {


private String _name;

private Object _value;


/** Creates a new instance of Parameter */

public ReportParameter(String name, Object value) {

_name = name;

_value = value;



public String getName() {

return _name;



public Object getValue() {

return _value;






In the template, you have just to write:


DataSourceProvider.create("CompanyCountry.jasper", new ReportParameter [] { new ReportParameter("CompanyName", $F{cpy.name}) } )


If you have any question, send me an email.

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...