Jump to content

Problem working with Bean DataSources


kogantivijay

Recommended Posts

Hi all,

 I am a newbee to JasperReports and Ireports. I have got a 5 List Objects created by my java application and the intention is to create a Report that would has data from all the 5Lists with couple of lists to be displayed as Group By and with Total/sum computations .

        I have done some study and my understanding is that this can be achieved using the DataSource  "JavaBeans Set DataSource" and creating a Master object wtih the each of the 5List Objects as a Attribute on the Master Object and create a report with 5 Subreports and while inserting the SubReport for the Step2:Connection exp select the radio button: use a JRDataSourceExpression and with value" new JRBeanCollectionDataSource($F{<ListElement>})" . I was creating the SubReport for these lists seperately and able to get fields from the report query,also its compiling propely but unable to run(as i cant pass data to this subreport). Now the problem is when i am trying to run the MainReport  i am getting this error:

Errors compiling C:ireportfilesireport3BeanReport.jasper!
Compilation exceptions: com.jaspersoft.ireport.designer.compiler.ErrorsCollector@4f1cf1 net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, calculator_BeanReport_1258468487525_97523: 169: unable to resolve class JRBeanCollectionDataSource   @ line 169, column 64.calculator_BeanReport_1258468487525_97523: 233: unable to resolve class JRBeanCollectionDataSource   @ line 233, column 64.calculator_BeanReport_1258468487525_97523: 297: unable to resolve class JRBeanCollectionDataSource   @ line 297, column 64. 3 errors     at net.sf.jasperreports.compilers.JRGroovyCompiler.compileUnits(JRGroovyCompiler.java:92)     at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:192)     at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:219)     at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:135)     at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:516)     at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:561)     at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:986)
Compilation running time: 328!

I confirmed that jasperreports-3.5.2.jar is avialable and it has the class JRBeanCollectionDataSource  as well.

My Query's :

a. Am i trying to implement in a proper way ie with subreports..etc or is there any other better way ?

b. If it is the correct method how to get rid of this error,I tried with 3.6.1 version as well just to make sure it's not wrong with jars.

FYI : I was trying to follow mostly as per the info in this site:

https://knol.google.com/k/nasir-qureshi/jasper-reports-working-with-beans-and/3az55pmbwf6s/3#An_Example_of_Using_An_ArrayList_As_A_Data_Source_For_JasperReports

I have already spent 2days to come to this point, Please help me out..



Post Edited by kogantivijay01 at 11/17/2009 14:40
Link to comment
Share on other sites

  • Replies 10
  • Created
  • Last Reply

Top Posters In This Topic

 

Hello.

I'm new in this too, but I know how to generate reports with a collection as datasource. So I'll try to help you...

I have take a look to the "working with beans reports", but I don't do it that way. I'll tell you how I do to generate a simple report; I think you can apply these to subreports, since I think the problem isn't there but in the way you're using ireport.

DESIGNING THE REPORT

1. Open iReport

2. Don't configure any database

3. File > New > Empty report and save it as a jrxml file.

4. In the report inspector, at "Fields", add some fields. The fields names must be the bean property names.

5. Drag and drop the fields to the detail view.

6. Clic on the "preview" button to generate the jasper file.

USING THE REPORT

See "Code" section.

Hope these works.

 

Regards,

Pepe Martínez

Code:
Collection<? extends Object> data=getData(); // Your data.JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(data);JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, dataSource);
Link to comment
Share on other sites

Hi pepe_martinez,

     The problem i have is that my bean is having a collection bean as one of its elements and showing this is the actual problem,

i was able to generate the preview of the bean with out the bean element but i am unable to figure out how to report it along with the bean element.

For Ex: I hae contact Object with elements:

String name;

long number;

List<Phone> phones;

List<Friends> friends;

Now the problem is how do i show a report with all these elements into the report.

 

Please let me know if you need any clarification.. Please help me out.. :(

regards,

vijay

Link to comment
Share on other sites

Good afternoon.

First of all, excuse me for the post before, because it didn't solve the problem...

But, I have just desgin and execute a report with a subreport, filled with beans.

The problem is that, when you set the data source expression for the subreport like this:

new JRBeanCollectionDataSource($F{collectionProperty})

iReport can't find the class. To solve this, you can invoke the constructor with the package path (i think there may exist another way, maybe somene can tell us):

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{collectionProperty})

This will solve the problem ;-) But, if you have any problem, please tell me.

Ah, you have to declare the field "collectionProperty" (phones and friends in your example).

Regards,

Pepe Martínez

 

 

Link to comment
Share on other sites

Hi Pepe Martínez,

 

Thanks for your reply, I have figured out the issue for the above error and yes you are right it is because of missing Full Name including the package.

can you tell me if the solution that i am following is a right approach for the above mentioned scenario ie Multiple List objects.

 

regards,

vijay

Link to comment
Share on other sites

Hi kogantivijay01:

Excuse me, I don't know if i have understood the scenario. I'm not sure if your scenario is I or II:

- SCENARIO I: You have a collection of objects ("main collection"). Each object has 5 attributes which are Lists ("subcollections"). You need to display everything in one report.

In this case, subreports are needed. As you said in your post before, the solution is to use a master report for the "main collection" and 5 subreports for each "subcollection".

- SCENARIO II: You have 5 collections of objects. You need to display eveithing in one report.

I think you have proposed this solution: define a new class with 5 attributes for theese 5 collections. You create an instance of that class and set the 5 collections; then this instance is added to a collection. Now we are in SCENARIO I, but "main collection" contains just one object. In my opinion, this is a smart solution.

 

There is another way to do this, but I don't like it... You can define a new class with 5 attributes. Each attribute would be of the type of the objects of each collections. For example: if you have (I'll do it for three collections):

Collection<Fruit> fruits=getFruitsCollection();

Collection<Vegetable> fruits=getVegetablesCollection();

Collection<Fish> fishes=getFishesCollection();

You can define a class:

public class Mix {

Fruit aFruit;

Vegetable aVegetable;

Fish aFish;

}

And generate a Collection<Mix>. Depending on the length of the tree collections, there could be some Mix objects that have aFruit == null, aVegetable == null or aFis == null.

You could design just one report for the Collection<Mix>.

 

This is another way, but, as I told you before, I don't like it. Yours is very smart and this, isn't.

 

Ah, and using bean datasources is the best way because this way you don't link the presentation layer (the reports) to a conrete persistence system or scheme. Defining bussiness objects is the best way.

Hope this was what you were asking for!

 

Regards,

Pepe Martínez

 

 

Link to comment
Share on other sites

Hi Pepe Martínez,

 

Thanks for your reply, I appreciate your views and would like to go-ahead as per the design that we discussed and i would post here a link to my blog once it is done as i find not a good list of resources for this along with all the views that we discussed so that others can take advantage of it.

 

I have one more question for you, this is just the extension/Modification of the Scenario 1 you have just defined :

- SCENARIO I: You have a collection of objects ("main collection"). Each object has 5 attributes which are Lists ("subcollections"). You need to display everything in one report.

 

The Question is now i want each object in the collection to have a separate report for itself.(Note that the object is again having 5 Lists as its elements)This way i can avoid calling the jasper for each object in the List by wrapping it in the another List. I hope i am clear at my question do let me know if u need any info.

 

btw sorry for the late reply, i was on vacation.

 

regards,

vijay

Link to comment
Share on other sites

Hello.

I THINK (maybe someone could confirm this) jasper can't do that; that's to say, I think jasper can't receive a list of N objects and generate N reports, one per object. So you should itereate over your collection and invoke jasper N times.

You can try to do the "common tasks" (for exmample, loading the jasper file) before the loop in order to gain on efficiency.

I think there are two doubts in this topic:

- How to set a "classpath" in iReport in order to avoid writing the full package name when seeting the datasource with a collections of beans. This way, you could write "new JRBeanCollectionDataSource($F{collectionProperty})" instead of "ew net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{collectionProperty})".

- Is it possible in iReports to generate N reports, given a collection of N objects (each object is a simple object, not a colecction, but has M lists, for M subreports).

 

Regards,

Pepe Martínez

 

Link to comment
Share on other sites

  • 3 weeks later...

 

Hi,
I am using iReport 3.7.0
I am not able to introduce javabeans datasource for subreport using subreport creation wizard. It gives me option to select the javabean datasource but does not give me option to select java bean class. consequently the next screen shows no fields to be selected. The subreport goes empty.

Please let me know if anybody knows the answer.

I followed the blog >> http://knol.google.com/k/nasir-qureshi/jasper-reports-working-with-beans-and/3az55pmbwf6s/3#
to create subreport using javabeans datasource

Thanks
Nayn

Link to comment
Share on other sites

  • 3 years later...

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