Announcement Announcement Module
Collapse
No announcement yet.
dataSource for JasperReports subreport Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • dataSource for JasperReports subreport

    I using in my application JasperReports. I trying pass data Beans through dataSource to the subreport (not by using SQL with parameter in WHERE clause), but all beans data are printed in first row of first parent record. It is like in jasperdemo application from Spring (this demo for subreport not works properly - data for subreport is idependent from parent - parameter $P{City} is unused). It is possible create subreports using dataSource instead of SQL queries? For SQL queries I must bypass Hibernate and my object model. To the parent report I must pass dataSource and to the subreport Connection (and SQL query in report design) - this is slightly incoherent.

    Witek.

  • #2
    Yes you definitely do not have to use direct sql for your subreports. You can set the datasource to anything you wish - it can be a parameter passed in from the model or even a field from the parent report.

    For example I have a report that displays a list of Orders and all the OrderLines inside each order. The parent report displays the Order headers, while the subreport displays the OrderLines.

    The jasperdemo with spring (IMHO) does not fully capture the "spirit" of subreports in that the subreport displays a fixed set of data that is not dependant on the parent report.

    If you give more details on what you're doing I probably can give you more specific advice.

    Comment


    • #3
      Thanks for quick replay and inclination for advice me.
      There are more details:
      Bean class for parent report:

      package web;

      import bus.JakiUrlop;

      public class JakiUrlopJR {
      private int id;
      private int rok;
      private int iloscDni;
      private int doWykorzystania;
      private String rodzaj;

      public JakiUrlopJR (JakiUrlop jakiUrlop) {
      this.id = jakiUrlop.getId();
      this.rok = jakiUrlop.getRok();
      this.iloscDni = jakiUrlop.getIloscDni();
      this.doWykorzystania = jakiUrlop.getIloscDni() - jakiUrlop.getDniWykorzystane();
      this.rodzaj = jakiUrlop.getRodzajUrlopu().getNazwa();
      }
      // seters and getters
      }

      Bean class for subreport:

      package web;
      import java.util.Date;
      import bus.Urlop;

      public class UrlopJR {
      private int jaki_urlop_id;
      private Date odDnia;
      private Date doDnia;
      private int ilosc;
      public UrlopJR(Urlop urlop) {
      this.jaki_urlop_id = urlop.getJakiUrlop().getId();
      this.odDnia = urlop.getDataOd();
      this.doDnia = urlop.getDataDo();
      this.ilosc = urlop.getDniRobocze();
      }
      //setters and getters
      }

      I set parameters for subreport in parent:
      <subreportParameter name="jaki_urlop_id">
      <subreportParameterExpression><![CDATA[$F{id}]]></subreportParameterExpression>
      </subreportParameter>
      <dataSourceExpression><![CDATA[$P{urlopySubReportDataSource}]]></dataSourceExpression>
      <subreportExpression class="net.sf.jasperreports.engine.JasperReport">< ![CDATA[$P{urlopySubReport}]]></subreportExpression>

      and in my Controller i fill beans with data:

      public ModelAndView urlopyRaportHtmlHandler (HttpServletRequest request, HttpServletResponse response) throws ServletException, SQLException {
      Osoba osoba = kadry.loadOsoba(RequestUtils.getIntParameter(reque st, "osobaId", 0));
      return new ModelAndView("urlopyReportHtml", getUrlopyModel(osoba));
      }
      private Map getUrlopyModel(Osoba osoba) throws SQLException {
      Collection jakieUrlopy = osoba.getJakieUrlopy();
      List lista = new ArrayList();
      List lista2 = new ArrayList();
      for (Iterator it = jakieUrlopy.iterator(); it.hasNext() {
      JakiUrlop jakiUrlop = (JakiUrlop)it.next();
      JakiUrlopJR jakiUrlopJR = new JakiUrlopJR(jakiUrlop);
      lista.add(jakiUrlopJR);
      Collection urlopy = jakiUrlop.getUrlopy();
      for (Iterator it2 = urlopy.iterator(); it2.hasNext() {
      Urlop urlop = (Urlop)it2.next();
      UrlopJR urlopJR = new UrlopJR(urlop);
      lista2.add(urlopJR);
      }
      }
      Map model = new HashMap();
      model.put("dataSource", lista);
      model.put("urlopySubReportDataSource", lista2);
      model.put("urlopujacy", osoba.getIdkadra() + " " + osoba.getImie() + " " + osoba.getNazwisko());
      return model;
      }

      Relation is between JakiUrlopJR.id and UrlopJR.jaki_urlop_id.
      In my report all data from UrlopJR bean are in first appearance of master record (JakiUrlopJR). I have no dependence between both beans.

      Witek.

      Comment


      • #4
        Many Spring Users can not resolve this problem

        Please help me resolve this problem.

        Comment

        Working...
        X