billn Posted July 17, 2017 Share Posted July 17, 2017 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 More sharing options...
sanbez Posted July 18, 2017 Share Posted July 18, 2017 You can use subreportIn main report define field corresponding getter for nested bean collection.For examplepublic 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 reportDefine in main jrxml DataSourceExpression for subreport like thisnew JRBeanCollectionDataSource($F{nestedBeans})define fields in subreport corresponding getters in MyNestedBean classfor example $F{firstStringField} with type java.lang.String HTH Link to comment Share on other sites More sharing options...
billn Posted July 18, 2017 Author Share Posted July 18, 2017 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 More sharing options...
sanbez Posted July 18, 2017 Share Posted July 18, 2017 You can use the search and not wait for advice.http://community.jaspersoft.com/questions/527187/nosuchmethodexception-unknown-property Link to comment Share on other sites More sharing options...
billn Posted July 18, 2017 Author Share Posted July 18, 2017 That fixed it. I set isUseFieldDescription to false when creating the JRBeanCollectionDataSource and the subreport displays fine now. Thanks again for your help! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now