Pass POJO to Subreport in Jasper Reports

I have a Hibernate POJO with 1. one-to-one relationship with another object 2.one-to-many association (collection) with another object

I am trying to create a Jasper report with these associations going to subreports. For many-to-one communications, I pass the data source as follows:

<subreport>
 <reportElement x="40" y="16" width="100" height="30"/>
 <dataSourceExpression>
   <![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{phones})]]>
 </dataSourceExpression>
 <subreportExpression>
    <![CDATA[$P{SUBREPORT_DIR} + "subreport1.jasper"]]>
 </subreportExpression>
</subreport>

It works great. And this is how I defined it for the one-to-one association

<subreport>
 <reportElement x="25" y="91" width="200" height="59"/>
 <dataSourceExpression>
   <![CDATA[new net.sf.jasperreports.engine.data.JRBeanArrayDataSource([$F{batchHeaderRecord}] as java.lang.Object[])]]>
 </dataSourceExpression>
 <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "batchHeaderReport.jasper"]]>
 </subreportExpression>
</subreport>

But this one does not work. Can someone please tell me where I am going wrong?

+5
source share
2 answers
new net.sf.jasperreports.engine.data.JRBeanArrayDataSource([$F{batchHeaderRecord}] as java.lang.Object[])

Invalid Java code. Just use

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(java.util.Collections.singleton($F{batchHeaderRecord}))

or

new net.sf.jasperreports.engine.data.JRBeanArrayDataSource(new Object[] {$F{batchHeaderRecord}})
+5
source

Install languagein Java so that the report fixes the groovy error.

+1

All Articles