Search code examples
hibernatejasper-reportssubreport

Pass a POJO to Subreport in Jasper Reports


I have a Hibernate POJO with 1.an one-to-one association to 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 association I am passing the datasource 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>

This works fine. And here is the way I defined it for 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 is not working. Can some one please let me know where I am going wrong?


Solution

  • new net.sf.jasperreports.engine.data.JRBeanArrayDataSource([$F{batchHeaderRecord}] as java.lang.Object[])
    

    is not valid 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}})