在JasperReports中使用hibernate的查询的步骤

在JasperReports中使用hibernate的查询的步骤

JasperReports 从1.2.0以后的版本开始 支持一些其它的查询的实现,如HQL和XPath,在此文中将介绍HQL在JasperReports中的应用。
1.要在报表中使用HQL,当然得将hibernate的一些包将入到你的类路径中,并且有相应的hbm.xml映射文件。
2.在设计报表时,可以直接将你的HQL写在报表模板文件中,注意有以下不同(红色字
<?xml version="1.0"?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd" [
<!ENTITY reportStyles SYSTEM "./ReportStyles.ent">
]>

<jasperReport
name="AddressesReport"
pageWidth="595"
pageHeight="842"
columnWidth="515"
columnSpacing="0"
leftMargin="40"
rightMargin="40"
topMargin="50"
bottomMargin="50"
whenNoDataType="AllSectionsNoDetail"
isTitleNewPage="false"
isSummaryNewPage="false">
<import value="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<style name="Arial_Normal" isDefault="true" fontName="Arial" fontSize="12" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Normal_Small" fontName="Arial" fontSize="9" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Bold" isDefault="false" fontName="Arial" fontSize="12" isBold="true" pdfFontName="Helvetica-Bold" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Italic" isDefault="false" fontName="Arial" fontSize="12" isItalic="true" pdfFontName="Helvetica-Oblique" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<parameter name="ReportTitle" class="java.lang.String"/>
<parameter name="CityFilter" class="java.util.List"/>
<queryString language="hql">
<![CDATA[
select address as address, document.id as documentId, document.total as documentTotal
from Address as address join address.documents as document
where city not in ($P{CityFilter})
order by address.city, address.lastName, address.firstName, address.id]]>
</queryString>
<field name="id" class="java.lang.Long">
<fieldDescription>address.id</fieldDescription>
</field>
<field name="firstName" class="java.lang.String">
<fieldDescription>address.firstName</fieldDescription>
</field>
<field name="lastName" class="java.lang.String">
<fieldDescription>address.lastName</fieldDescription>
</field>
<field name="street" class="java.lang.String">
<fieldDescription>address.street</fieldDescription>
</field>
<field name="city" class="java.lang.String">
<fieldDescription>address.city</fieldDescription>
</field>
<field name="documentId" class="java.lang.Long"/>
<field name="documentTotal" class="java.lang.Double"/>
<variable name="addressTotal" class="java.lang.Double" resetType="Group" resetGroup="AddressGroup" calculation="Sum">
<variableExpression>$F{documentTotal}</variableExpression>
</variable>
<group name="AddressGroup">
<groupExpression>
$F{id}
</groupExpression>
<groupHeader>
<band height="25">
<frame>
<reportElement x="0" y="0" width="375" height="21" mode="Opaque" backcolor="#E0E0E0"/>
<textField isStretchWithOverflow="true">
<reportElement x="5" y="3" width="160" height="15"/>
<textFieldExpression class="java.lang.String">
$F{firstName} + " " + $F{lastName} + " (ID: " + $F{id} + ")"
</textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="210" y="3" width="160" height="15"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.String">
$F{street} + "," + $F{city}
</textFieldExpression>
</textField>
</frame>
</band>
</groupHeader>
<groupFooter>
<band height="25">
<line>
<reportElement x="20" y="0" width="475" height="1" forecolor="#808080"/>
<graphicElement pen="Thin"/>
</line>
<staticText>
<reportElement x="20" y="2" width="70" height="15" style="Arial_Normal_Small"/>
<text>Total</text>
</staticText>
<textField>
<reportElement x="100" y="2" width="50" height="15" style="Arial_Normal_Small"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Integer">
$V{AddressGroup_COUNT}
</textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" pattern="#.00">
<reportElement x="255" y="2" width="100" height="15" style="Arial_Normal_Small"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Double">
$V{addressTotal}
</textFieldExpression>
</textField>
</band>
</groupFooter>
</group>
<title>
<band height="50">
<line>
<reportElement x="0" y="0" width="515" height="0" forecolor="black"/>
</line>
<textField isBlankWhenNull="true">
<reportElement x="0" y="10" width="515" height="30" style="Arial_Normal"/>
<textElement textAlignment="Center" lineSpacing="Single">
<font size="22"/>
</textElement>
<textFieldExpression class="java.lang.String">$P{ReportTitle}</textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="13">
<staticText>
<reportElement x="20" y="0" width="70" height="12" style="Arial_Normal_Small" isPrintRepeatedValues="false" isPrintInFirstWholeBand="true" printWhenGroupChanges="AddressGroup"/>
<text>Document ID</text>
</staticText>
<textField>
<reportElement x="100" y="0" width="50" height="12" style="Arial_Normal_Small"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Long">
$F{documentId}
</textFieldExpression>
</textField>
<staticText>
<reportElement x="180" y="0" width="70" height="12" style="Arial_Normal_Small" isPrintRepeatedValues="false" isPrintInFirstWholeBand="true" printWhenGroupChanges="AddressGroup"/>
<text>Document Total</text>
</staticText>
<textField isStretchWithOverflow="true" pattern="#.00">
<reportElement x="255" y="0" width="100" height="12" style="Arial_Normal_Small"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Double">
$F{documentTotal}
</textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="40">
<printWhenExpression>$P{IS_IGNORE_PAGINATION}.booleanValue()?Boolean.FALSE:Boolean.TRUE</printWhenExpression>
<line>
<reportElement x="0" y="10" width="515" height="0"/>
</line>
<textField>
<reportElement x="200" y="20" width="80" height="15"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.String">
"Page " + String.valueOf($V{PAGE_NUMBER}) + " of"
</textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="280" y="20" width="75" height="15"/>
<textElement textAlignment="Left"/>
<textFieldExpression class="java.lang.String">
" " + String.valueOf($V{PAGE_NUMBER})
</textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>


填充报表
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession()
Transaction transaction = session.beginTransaction();
//报表参数
Map parameters = new HashMap();
parameters.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, session);
parameters.put("ReportTitle", "Address Report");
List cityFilter = new ArrayList(3);
cityFilter.add("Boston");
cityFilter.add("Chicago");
cityFilter.add("Oslo");
parameters.put("CityFilter", cityFilter);
parameters.put("OrderClause", "city");

for(int i = 0; i < reportNames.length; i++)
{
long start = System.currentTimeMillis();
JasperFillManager.fillReportToFile(reportNames[i] + ".jasper", params);
System.err.println("Report : " + reportNames[i] + ". Filling time : " + (System.currentTimeMillis() - start));
}

transaction.rollback();
session.close();