Jump to content
We've recently updated our Privacy Statement, available here ×

Tutorial for using Nested JavaBeans


billn

Recommended Posts

Hi-

I would like to create a report that uses a java bean that has a nested java bean defined in it. The parent bean has first name, last name, an id, and a list of the nested bean type, all with associated getters and setters. The nested bean has four fields. A single date field, and 3 string fields, also with getters and setters. The report has to output the parent fields, and the fields for each list element of the nested bean type. Can someone point me to a tutorial or sample that gives the steps for doing this using the jasper reports API? I have a template that prints the parent fields just fine, I'm just unsure of how to access the nested fields from the template.

Thanks in advance.

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

You can use subreport

In main report define field corresponding getter for nested bean collection.
For example

public class MyBean() {

private List<MyNestedBean> mysuperpuperNestedBeans;

private Long id;

public Long getId() {

return id;

}

public List<MyNestedBean> getNestedBeans() {
     return mysuperpuperNestedBeans;
}

}

 

 

public class MyNestedBean() {

private String firstStringField;

public String getFirstStringField() {

return firstStringField;

}

}


in jrxml define fields
$F{id} with type Long
$F{nestedBeans} with type java.util.Collectioin  (name of field corresponds getter getNestedBeans)

make subreport in detail band of main report
Define in main jrxml DataSourceExpression for subreport like this

new JRBeanCollectionDataSource($F{nestedBeans})

define fields in subreport corresponding getters in MyNestedBean class

for example $F{firstStringField} with type java.lang.String

 

HTH

Link to comment
Share on other sites

Thanks that helped and I am much further along after implementing your suggestion. I am getting this exception though. I have defined the corresponding field getters in the nested bean and created fields with the same names in the subreport. I don't understand why it is saying, "Unknown property". As a test, I made sure that I could display the subreport standalone with data from the nested beans, and it works fine. The exception only occurs when the subreport is called from the main report.

 

Caused by: java.lang.NoSuchMethodException: Unknown property '' on class 'class gov.doh.hcs.chrc.internal.employer.datatransfer.EmployerLetterDTO$OpenCharge'

at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1313)

at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:762)

at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:837)

at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)

at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)

... 15 more

 

In the main report:

Subreport Expression is set to $P{openChargesSubReport}

Data Source Expression is set to: new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{openCharges})

 

In the subreport the fields are called:

arrestDate

statute

summary

offenseLevel

jurisdiction

 

Any additional suggestions you can provide are greatly appreciated.

Link to comment
Share on other sites

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...