Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

jasper reports with subreports  RSS feed

subu ananthram
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I want to create a subreport with in a report.
I am trying to create a jaspersubreport and I want to integrate hql into it.
I dont know how to do so... Can anyone help me
This is what I have tried to do

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 net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
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;

// 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();
//Read query for sugbreport
String hql="from HogwartStudentHouseDetails";
List subReportList=session.createQuery(hql).list();
Iterator itr=subReportList.iterator();
HogwartStudentHouseDetails ht=(HogwartStudentHouseDetails);
//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");
// Actual contact insertion will happen at this step
System.out.println("Inside finally");



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



[added code tags]
[ March 16, 2006: Message edited by: Jeanne Boyarsky ]
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!