JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)
|
|
|
|
JasperReports - EJBQL Sample (version 4.6.0) | ![]() |
|
|
|
Main Features in This Sample | |
| EJBQL Query Executer |
|
|
Secondary Features | |
Query Executers |
![]() | ![]() | ![]() | ![]() |
|
|
||||
top | |||||||||
|
|||||||||
![]() | EJBQL Query Executer | Documented by Sanda Zaharia | |||||||
|
|||||||||
| Description / Goal |
| How to fill reports using embedded EJBQL queries. | ||||||
| Since |
| 1.2.3 | ||||||
| Other Samples |
|
|
||||||
|
|||||||||
|
The EJB QL/JPA Query Executer
The EJB QL report query executer adds support for reporting on EJB 3.0 persistent entities data. For an EJB QL query in a report, the query executer will use the EJB 3.0 Java Persistence API to execute the query against an entity manager provided at runtime, and use the query result as a data source for the report. The built-in EJB QL query executer is registered by default for queries having EJBQL or ejbql set as their language. This mapping can be changed by using the related JasperReports
properties (see properties in the category
net.sf.jasperreports.query.executer.factory.{language}
).
Two built-in parameters are involved in the query execution:
<property name="net.sf.jasperreports.ejbql.query.hint.cacheType" value="Shared"/>A separate report property can be used to paginate the query result in order to control the amount of Java heap space used by the query executer while filling the report. The property can be set in the following manner: <property name="net.sf.jasperreports.ejbql.query.page.size" value="500"/>meaning that the query result will be fetched in chunks containing 500 rows each. The pagination is achieved via the javax.persistence.Query.setMaxResults()
and setFirstResult() methods. Obviously, using pagination could result in performance loss. Therefore enabling it is primarily recommended when the query
results are very large.
The result of the query execution is sent to a data source implementation, which iterates over it and extracts report field values. Fields are mapped to specific values in the query result by specifying the mapping as field description or field name. The JPA data source can handle two types of query results:
The movie database sample in the demo/samples/ejbql directory is structured as follows:
reports/JRMDbReport.jrxml master report is structured to present movies in the database along with their additional informations, such as
casting, awards, quotes, etc. Casts are managed separately in the reports/JRMDbCastSubreport.jrxml template, and the rest of additional
information is provided by the reports/JRMDbVariaSubreport.jrxml
Looking into the reports/JRMDbReport.jrxml , one could notice the specific hint properties, the EJB QL query syntax, report fields declarations
and how the subreports were set:
... <property name="net.sf.jasperreports.ejbql.query.hint.fetchSize" value="50"/> <property name="net.sf.jasperreports.ejbql.query.page.size" value="100"/> <import value="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/> ... <queryString language="ejbql"> <![CDATA[SELECT m FROM Movie m WHERE m.releaseDate BETWEEN $P{DateFrom} AND $P{DateTo} ORDER BY $P!{OrderClause}]] > </queryString> <field name="id" class="java.lang.Integer"/> <field name="director.name" class="java.lang.String"/> <field name="title" class="java.lang.String"/> <field name="genre" class="java.lang.String"/> <field name="releaseDate" class="java.sql.Date"/> <field name="cast" class="java.util.Collection"/> ... <detail> <band height="45"> ... <subreport> <reportElement positionType="Float" x="15" y="25" width="245" height="20" isRemoveLineWhenBlank="true" backcolor="#99CCFF"/> <dataSourceExpression><![CDATA[new JRBeanCollectionDataSource($F{cast})]] ></dataSourceExpression> <subreportExpression class="java.lang.String"><![CDATA["JRMDbCastSubreport.jasper"]] ></subreportExpression> </subreport> <subreport> <reportElement positionType="Float" x="270" y="25" width="245" height="20" isRemoveLineWhenBlank="true" backcolor="#99CCFF"/> <subreportParameter name="MovieId"> <subreportParameterExpression><![CDATA[$F{id}]] ></subreportParameterExpression> </subreportParameter> <subreportParameter name="JPA_ENTITY_MANAGER"> <subreportParameterExpression><![CDATA[$P{JPA_ENTITY_MANAGER}]] ></subreportParameterExpression> </subreportParameter> <subreportExpression class="java.lang.String"><![CDATA["JRMDbVariaSubreport.jasper"]] ></subreportExpression> </subreport> </band> </detail> ...The Movie entities provide an id , a director (who is a Person entity), a title ,
a genre , releaseDate and a collection of cast entities. Notice the report field
director.name that refers to the name property in the
The casts collection in the cast field is passed as a JRBeanCollectionDataSource to the casting subreport.
The Varia subreport has no expression for data source/connection, but takes the built-in JPA_ENTITY_MANAGER parameter
into consideration, and the movie ID, in order to identify the Varia entities related to the movie.
The reports/JRMDbCastSubreport.jrxml provides no query, because the data source comes already prepared here. There are two fields
declared in the report:
<field name="actor.name" class="java.lang.String"/> <field name="character" class="java.lang.String"/>Again, the actor.name field references the name property in the actor entity of type Person .
In the reports/JRMDbVariaSubreport.jrxml one could notice the presence of an EJB QL query and field descriptions based on column positions:
... <parameter name="MovieId" class="java.lang.Integer"/> <queryString language="ejbql"> <![CDATA[SELECT v.type, v.description FROM Varia v WHERE v.movie.id = $P{MovieId} ORDER BY v.importance]] > </queryString> <field name="type" class="java.lang.String"> <fieldDescription><![CDATA[COLUMN_1]] ></fieldDescription> </field> <field name="description" class="java.lang.String"> <fieldDescription><![CDATA[COLUMN_2]] ></fieldDescription> </field> ...Running the Sample Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/hsqldb within the JasperReports source project and run the > ant runServer command.
It will start the HSQLDB server shipped with the JasperReports distribution package. Let this terminal running the HSQLDB server.
Open a new command prompt/terminal window and set the current folder to demo/samples/ejbql within the JasperReports source project and run the > ant test view command.
It will generate all supported document types containing the sample report in the demo/samples/ejbql/build/reports directory.
Then the report will open in the JasperReports internal viewer. |
||||||||
|
|
© 2001- Jaspersoft Corporation www.jaspersoft.com |