Jump to content
We've recently updated our Privacy Statement, available here ×

jasper with hibernate with subreport


Recommended Posts

By: pri - priyasubu

jasper with hibernate with subreport

2006-03-15 21:34

Hi I want to create a subreport with in a report.

I followed the tutorial in the ireport web site.Now say I have a main report say hogwart's student details and in the subreport I want to show the subjects taken or something like that how do I do it.

I want to use hql

regards

pri

 

 

 

By: pri - priyasubu

RE: jasper with hibernate with subreport

2006-03-16 17:20

Can anyone help me

This is what I have tried to do

Code

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import org.hibernate.Transaction;

import org.hibernate.Query;

import java.util.List;

import java.lang.ClassLoader;

import java.util.HashMap;

import java.io.InputStream;

import net.sf.jasperreports.engine.design.JasperDesign;

import net.sf.jasperreports.engine.JasperCompileManager;

import net.sf.jasperreports.engine.JasperReport;

import net.sf.jasperreports.engine.xml.JRXmlLoader;

import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import net.sf.jasperreports.engine.JasperPrint;

import net.sf.jasperreports.engine.JasperFillManager;

import net.sf.jasperreports.engine.JasperExportManager;

import java.util.Iterator;

import java.util.ArrayList;

 

 

public class TestJasperSubReports {

 

public static void main(String[] args) {

Session session = null;

Transaction tx=null;

 

try{

// This step will read hibernate.cfg.xml and prepare hibernate for

// use

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

session =sessionFactory.openSession();

ArrayList tmpList=new ArrayList();

tx = session.beginTransaction();

HashMap params=new HashMap();

params.put("title","dd");

//Read query for sugbreport

String hql="from HogwartStudentHouseDetails";

List subReportList=session.createQuery(hql).list();

Iterator itr=subReportList.iterator();

while(itr.hasNext()){

HogwartStudentHouseDetails ht=(HogwartStudentHouseDetails)itr.next();

System.out.println("--"+ht.getStudentHouse());

}

//Jasper subreport prep

InputStream subStream= TestJasper.class.getResourceAsStream("/JasperSub1.jrxml");

JasperDesign subReportDesign=JRXmlLoader.load(subStream);

JasperReport subCompiled=JasperCompileManager.compileReport(subReportDesign);

System.out.println("subReportSize is"+subReportList.size());

JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(subReportList);

 

JasperPrint printSubreport = JasperFillManager.fillReport(subCompiled, params, ds);

JasperExportManager.exportReportToHtmlFile(printSubreport, "hogwart.html");

 

//Read query for main report

String hibernateql="from HogwartStudentDetails";

 

List mainList=session.createQuery(hibernateql).list();

 

 

//Prepare main report

InputStream mainStream= TestJasper.class.getResourceAsStream("/jasperhibernate.jrxml");

JasperDesign mainDesign=JRXmlLoader.load(mainStream);

JasperReport mainCompiled=JasperCompileManager.compileReport(mainDesign);

JRBeanCollectionDataSource mainDS = new JRBeanCollectionDataSource(mainList);

JasperPrint printMainreport = JasperFillManager.fillReport(mainCompiled, params, mainDS);

 

JasperExportManager.exportReportToHtmlFile(printMainreport, "hogwartstudent.html");

 

System.out.println("in here");

 

 

 

 

 

 

 

}catch(Exception e){

System.out.println("inside catch");

System.out.println(e.getMessage());

}finally{

// Actual contact insertion will happen at this step

System.out.println("Inside finally");

session.flush();

session.close();

 

}

 

}}

 

 

Both the hogwart.html and hogwartStudents.html are ok they print the righ tdata now how do I make it a subreport in Jasper

this is my xml

 

JasperSub1.jrxml

<?xml version="1.0" encoding="UTF-8" ?>

<!-- Created with iReport - A designer for JasperReports -->

<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport

name="JasperSub1"

columnCount="1"

printOrder="Vertical"

orientation="Portrait"

pageWidth="595"

pageHeight="842"

columnWidth="535"

columnSpacing="0"

leftMargin="30"

rightMargin="30"

topMargin="20"

bottomMargin="20"

whenNoDataType="NoPages"

isTitleNewPage="false"

isSummaryNewPage="false">

<property name="ireport.scriptlethandling" value="0" />

<property name="ireport.encoding" value="UTF-8" />

<import value="java.util.*" />

<import value="net.sf.jasperreports.engine.*" />

<import value="net.sf.jasperreports.engine.data.*" />

 

<parameter name="studentId" isForPrompting="false" class="java.lang.String">

<defaultValueExpression ><![CDATA["1"]]></defaultValueExpression>

</parameter>

<queryString language="xPath"><![CDATA[from HogwartStudentHouseDetails where studentId=${studentId}]]></queryString>

 

<field name="studentHouse" class="java.lang.String"/>

<field name="studentId" class="java.lang.String"/>

 

<background>

<band height="0" isSplitAllowed="true" >

</band>

</background>

<title>

<band height="50" isSplitAllowed="true" >

<staticText>

<reportElement

mode="Transparent"

x="310"

y="6"

width="164"

height="33"

forecolor="#000000"

backcolor="#FFFFFF"

key="staticText-1"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="18" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<text><![CDATA[studentHouse]]></text>

</staticText>

<staticText>

<reportElement

mode="Transparent"

x="36"

y="5"

width="178"

height="34"

forecolor="#000000"

backcolor="#FFFFFF"

key="staticText-2"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="18" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<text><![CDATA[studentId]]></text>

</staticText>

</band>

</title>

<pageHeader>

<band height="50" isSplitAllowed="true" >

</band>

</pageHeader>

<columnHeader>

<band height="30" isSplitAllowed="true" >

</band>

</columnHeader>

<detail>

<band height="100" isSplitAllowed="true" >

<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >

<reportElement

mode="Transparent"

x="298"

y="9"

width="215"

height="42"

forecolor="#000000"

backcolor="#FFFFFF"

key="textField-2"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$F{studentHouse}]]></textFieldExpression>

</textField>

<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >

<reportElement

mode="Transparent"

x="20"

y="22"

width="167"

height="36"

forecolor="#000000"

backcolor="#FFFFFF"

key="textField-3"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$F{studentId}]]></textFieldExpression>

</textField>

</band>

</detail>

<columnFooter>

<band height="30" isSplitAllowed="true" >

</band>

</columnFooter>

<pageFooter>

<band height="50" isSplitAllowed="true" >

</band>

</pageFooter>

<lastPageFooter>

<band height="50" isSplitAllowed="true" >

</band>

</lastPageFooter>

<summary>

<band height="50" isSplitAllowed="true" >

</band>

</summary>

</jasperReport>

 

MasterReport calling subreport

<?xml version="1.0" encoding="UTF-8" ?>

<!-- Created with iReport - A designer for JasperReports -->

<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport

name="jasperhibernate"

columnCount="1"

printOrder="Vertical"

orientation="Portrait"

pageWidth="595"

pageHeight="842"

columnWidth="535"

columnSpacing="0"

leftMargin="30"

rightMargin="30"

topMargin="20"

bottomMargin="20"

whenNoDataType="NoPages"

isTitleNewPage="false"

isSummaryNewPage="false">

<property name="ireport.scriptlethandling" value="0" />

<property name="ireport.encoding" value="UTF-8" />

<import value="java.util.*" />

<import value="net.sf.jasperreports.engine.*" />

<import value="net.sf.jasperreports.engine.data.*" />

 

<parameter name="studentId" isForPrompting="false" class="java.lang.String">

<defaultValueExpression ><![CDATA["1"]]></defaultValueExpression>

</parameter>

<queryString language="hql"><![CDATA[from HogwartStudentDetails]]></queryString>

 

<field name="studentId" class="java.lang.String"/>

<field name="studentName" class="java.lang.String"/>

 

<background>

<band height="0" isSplitAllowed="true" >

</band>

</background>

<title>

<band height="50" isSplitAllowed="true" >

<staticText>

<reportElement

mode="Transparent"

x="76"

y="7"

width="333"

height="41"

forecolor="#000000"

backcolor="#FFFFFF"

key="staticText-1"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial Black" pdfFontName="Helvetica" size="18" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<text><![CDATA[Hogwart Student Register

]]></text>

</staticText>

</band>

</title>

<pageHeader>

<band height="50" isSplitAllowed="true" >

<staticText>

<reportElement

mode="Transparent"

x="282"

y="13"

width="192"

height="25"

forecolor="#000000"

backcolor="#FFFFFF"

key="staticText-3"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial Black" pdfFontName="Helvetica" size="12" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<text><![CDATA[Name]]></text>

</staticText>

<staticText>

<reportElement

mode="Transparent"

x="78"

y="11"

width="124"

height="23"

forecolor="#000000"

backcolor="#FFFFFF"

key="staticText-5"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica-Bold" size="12" isBold="true" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<text><![CDATA[id]]></text>

</staticText>

</band>

</pageHeader>

<columnHeader>

<band height="30" isSplitAllowed="true" >

</band>

</columnHeader>

<detail>

<band height="200" isSplitAllowed="true" >

<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >

<reportElement

mode="Transparent"

x="277"

y="26"

width="193"

height="22"

forecolor="#000000"

backcolor="#FFFFFF"

key="textField-2"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="12" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$F{studentName}]]></textFieldExpression>

</textField>

<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >

<reportElement

mode="Transparent"

x="76"

y="33"

width="129"

height="21"

forecolor="#000000"

backcolor="#FFFFFF"

key="textField-3"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="12" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$F{studentId}]]></textFieldExpression>

</textField>

<subreport isUsingCache="true">

<reportElement

mode="Opaque"

x="69"

y="110"

width="405"

height="44"

forecolor="#000000"

backcolor="#FFFFFF"

key="subreport-1"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<subreportParameter name="studentId">

<subreportParameterExpression><![CDATA[$F{studentId}]]></subreportParameterExpression>

</subreportParameter>

<subreportExpression class="java.lang.String"><![CDATA["C:/cygwin/home/Priya/work/HibernateJasperProject/JasperSub1.jasper"]]></subreportExpression>

</subreport>

<staticText>

<reportElement

mode="Transparent"

x="71"

y="72"

width="430"

height="18"

forecolor="#000000"

backcolor="#FFFFFF"

key="staticText-6"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<text><![CDATA[-------------------------------------------------------------------------]]></text>

</staticText>

</band>

</detail>

<columnFooter>

<band height="30" isSplitAllowed="true" >

</band>

</columnFooter>

<pageFooter>

<band height="50" isSplitAllowed="true" >

</band>

</pageFooter>

<lastPageFooter>

<band height="50" isSplitAllowed="true" >

</band>

</lastPageFooter>

<summary>

<band height="50" isSplitAllowed="true" >

</band>

</summary>

</jasperReport>

 

Thanks

Pri

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

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