Saturday, April 8, 2006

Using other query languages in JasperReports



In version 1.2.0 of JasperReports - The most popular open source reporting engine in the world - support for other query languages, including HQL and XPath, is added.

This means you can use HQL directly in report templates! The report engine will create a HibernateSessionFactory which uses your Hibernate configuration and mappings to connect to the database, build the SQL statement and return the mapped Java objects. Example of HQL queryString in report template:

<queryString language="hql">
<![CDATA[
from Cat cat
where cat.weight > ($P{CatWeight})]]>
</queryString>

Notice the language="hql" which indicates the report engine to use HQL instead of plain JDBC SQL. JasperReports introduced the JRQueryExecuterFactory and JRQueryExecuter interfaces to incorporate new/additional query languages easily by - just - implementing them. Currently there are query language implementations for JDBC, Hibernate and Xpath.

As the Java Persistence API, part of EJB 3.0/JEE 5.0, is becoming the standard I decided to create a patch to add support for JPA/EJBQL in JasperReports. The code is already submitted to the JasperReports team and I hope it will be included soon in a future release.

When released I will add a step-by-step example on using EJBQL queries in report templates.

No comments: