Tutorial for using Nested JavaBeans

1

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.

billn's picture
10
Joined: Feb 24 2017 - 4:58am
Last seen: 1 week 16 hours ago

4 Answers:

1

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

sanbez's picture
716
Joined: Jan 11 2011 - 2:06am
Last seen: 3 hours 23 min ago
0
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.
billn's picture
10
Joined: Feb 24 2017 - 4:58am
Last seen: 1 week 16 hours ago
0

 

You can use the search and not wait for advice.

http://community.jaspersoft.com/questions/527187/nosuchmethodexception-u...

 

sanbez's picture
716
Joined: Jan 11 2011 - 2:06am
Last seen: 3 hours 23 min ago
0

That fixed it. I set isUseFieldDescription to false when creating the JRBeanCollectionDataSource and the subreport displays fine now. Thanks again for your help!

billn's picture
10
Joined: Feb 24 2017 - 4:58am
Last seen: 1 week 16 hours ago
Feedback
randomness