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

Subreport using JavaBeans classes


danbos

Recommended Posts

 Hi everyone,

I have just started using JasperReports and I am having some issues accessing certain fields from my data source.

Basically i have 2 classes as a Datasource, Company and Person.
Person is a very simple class with few fields, Company is also quite simple but it contains an ArrayList<Person> containing a list of Person class objects.

 

My factory class creates 15 instances of the Person class, adds 5 them to 1 ArrayList and there is 3 ArrayLists in total.
3 Company objects are then initiated with the 3 created ArrayLists. 

Factory class then adds the 3 instances of the Company class into another ArrayList which is passed to JasperReports.

So basically its a nested ArrayList that is getting passed.


In jasper reports I have setup datasource properly (at least the test says so) and I am able to see all the fields when I edit the query.  I can access information from company fields but not from the Person field.

 

This is the guide I followed but a lot of options have changed since then
http://knol.google.com/k/jasper-reports-working-with-beans-and-sub-report#Specify_the_Data_Source_for_the_Sub_Report 

 

 

Does anyone know why this might be happening and how do I go about fixing it?
Thanks in advance. 

Link to comment
Share on other sites

  • 2 weeks later...
  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Danbos,

  I have been trying to learn how to use JasperSoft's JasperReports and iReport off and on for the past month and I am actually in the same boat as you are.  I found that tutorial posted for use with iReport 3.0.0 and decided to see if I could mold it into use with version 4.1.1, but that hasn't worked so far.

  That article was the only piece of information I could find about using a nested ArrayList structure as a datasource that my program is set up for.  I am filling the ArrayList structures from a database, but the connection is on a separate layer of the program that is not reachable from the layer I am wanting to generate the report from.

  Currently I am able to access the top layer of the nested ArrayList, but the second layer is unreachable by means of subreport unless I change its get method to static and treat it as a new datasource.  Alas, this change doesn't produce the requested results.

  My latest thoughts of a solution include finding the version 3.0.0 iReport that was used in the article and trying to follow through it again.

  If you or anyone happens to find out how to create reports with nested ArrayLists please share the info.  I'll do the same if I figure it out as well.

  Thanks everyone.

Link to comment
Share on other sites

Hi Danbos,

Heres a few things that I have needed.

DataSource Binding to bean containing the SubreportData as a BeanCollection in a Field:

<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{fieldNameToBeUsed})]]></dataSourceExpression>

 

If you have trouble finding the Sub JasperReport when creating it from your project. You can put the whole .jasper file as a parameter.

You will use parameter Class JasperReport. To be able to use it in Ireport you can use Defaultvalue Expression. You will need to compile each raport by hand if you use this but it is atleast the way to find reports in projects where say EJB container handles the reports and they are coming from another Document store system.


<subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[$P{parameterNameContainingTheSubreport}]]></subreportExpression>

 

And the Parameter with defaultValue:

<parameter name="parameterNameContainingTheSubreport" class="net.sf.jasperreports.engine.JasperReport" isForPrompting="false">
        <defaultValueExpression><![CDATA[net.sf.jasperreports.engine.util.JRLoader.loadObject(new java.io.FileInputStream(new java.io.File($P{SUBREPORT_DIR}+"YourSubRaportFile.jasper")))]]></defaultValueExpression>
    </parameter>

 

In the Subreport you need to add the fields from the subBean and they should be usable. You use the Report Query editor and choose Java Bean DataSource. Choose your class and read attributes and add them as needed.

 

I have been using Jasper for just under a month now but I hope this helps. At least it works for me in iReport 4.1.1 and I do compile it for compatibility with 3.7.4 as our project libs are still a bit old.

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