Tutorial First Example

Introduction

In this tutorial we will create a jasper report that uses an hibernate datasource to build data. Our example project is inside the example folder, with a ready to use hsql db with some data for our demo system “PetHotel”. Also look at:


example/reports: some reports created with ireport with hibernate

example/hbm: hibernate mappings used by this example

example/bin : binary classes of pethotel system

example/conf: Configuration for HSQL and Postgres database.

example/driver: database drivers

example/db: HSQL database

example/sql: SQL database creation

/hibernate: hibernate libs





1 – Put in the classpath driver, hibernate mappings,hibernate config and javabeans implementations









In this example we need to add the db folder, where is the HSQL database data. This will not be needed with other databases.



2 – Create a report named Test

3- Create a datasource provider with class HBProvider:



4-Enter a query in the new Hibernate Datasource panel:






Press Refresh button to see the selected object in the tree:





Select fields to add:





Rename “Name” to “PetName”:




Expand Owners Name and add:





Select All fields in the table and click Ok: You must select all fields, or they will be erased.





5- Create a simple report layout with the fields:





6- Run using datasource, you will get the nice output:







Change the HQL using alias in the select clause:




The list box right to the refresh button contains the parameters.


Add petOwner to select clause:





look at the list:





Choose the owner object:




Now you can add fields from both owner and pet.

Add Name,Login,Billing Country and Billing Street




Add the PetSet relation:



Select all fields and press “ok”. You must select all fields, or they will be erased.


Now Let's build the report. This is our example layout:




The gray box in the center will be our sub-report. Double click it to edit.





In the parameters, we sent the master's parameters.

In the connection datasource we call the getSubDS method that creates a datasource with the same session, over the persistent collection PetSet, with the filter “order by this.name”.

This will be translated to Hibernate as:


Query hquery = sess.createFilter(<petset>, <where/orderby clause>);


* REPORT_DATA_SOURCE is can be REPORT_DATASOURCE in some jasper versions. My ireport displays REPORT_DATASOURCE, but jasper uses REPORT_DATA_SOURCE.

Now, we set the subreport tab:






Entering sub report fields:


First the name of Specie. Rename to SpecieName.

Notice that the query entered will be used only for stand alone running. When called from the master report, it will be ignored.


Them, pet name and numeration of the room:





Now, build the Interface:




Don't forget to remove margins.

Save, and run:





Now, let's try to run both reports. Open the master report and run:




That's it.