No data shown on subreport(Subreport using Java beans)

0

Good day!

I'm new to Jaspersoft and all the articles I've read about using javabeans as datasource for reports with subreports are not working. The main report has data shown but the subreport is blank even if the datasource is not empty and/or AllSectionsNoDetail is selected. 

I've tried passing the subreport data as param and as field. Both approaches didnt work. It would always say, "WARN  JRJdbcQueryExecuter:129 - The supplied java.sql.Connection object is null."

I know these is TL;DR but for those who wants to share please answer. 

Thanks guys!

POJO:

package com.cmsbeta.hibernate.domain;
 
import java.math.BigDecimal;
...
 
public class ChargeTypeAndEntries {
 
private Long id = CptiUtil.generatePrimaryKey();
private DateTime createDate;
private User createdBy;
private String code;
private String description;
private BigDecimal amount;
private Integer vatComputation;
private Integer status = 1;
private Integer isSpecial = 0;
private List<Entry> entries;
 
public Long getId() {
return id;
}
 
public void setId(Long id) {
this.id = id;
}
 
....
public List<Entry> getEntries() {
return entries;
}
 
public void setEntries(List<Entry> entries) {
this.entries = entries;
}
}
 
Report Filter:
 
package com.cmsbeta.web.function.reportfilter.page;
 
import java.util.ArrayList;
...
 
public class ChargeTypeListReportFilterPage extends CptiBasePage {
@SpringBean
private ChargeTypeService chargeTypeService;
@SpringBean
private EntryService entryService;
 
 
private String searchCode;
private String searchDescription;
private Boolean searchIncludeInactive;
 
 
public ChargeTypeListReportFilterPage() {
setPageTitle("report.title.chargetypelist");
 
TextField<String> code = new TextField<String>("code", new PropertyModel<String>(this, "searchCode"));
TextField<String> description = new TextField<String>("description", new PropertyModel<String>(this, "searchDescription"));
CheckBox includeInactive = new CheckBox("includeInactive", new PropertyModel<Boolean>(this, "searchIncludeInactive"));
 
 
Form<?> form = new Form<Void>("form");
form.add(new AttributeAppender("target", Model.of("_blank")));
 
SubmitLink submit = new SubmitLink("submit") {
@Override
public void onSubmit() {
List<ChargeType> chargeTypes = chargeTypeService.search(searchCode, searchDescription, null, searchIncludeInactive);
List<ChargeTypeAndEntries> data = new ArrayList<ChargeTypeAndEntries>();
 
for(ChargeType chargeType : chargeTypes){
ChargeTypeAndEntries cae = new ChargeTypeAndEntries();
cae.setId(chargeType.getId());
cae.setAmount(chargeType.getAmount());
cae.setCode(chargeType.getCode());
cae.setCreateDate(chargeType.getCreateDate());
cae.setCreatedBy(chargeType.getCreatedBy());
cae.setDescription(chargeType.getDescription());
cae.setIsSpecial(chargeType.getIsSpecial());
cae.setStatus(chargeType.getStatus());
cae.setVatComputation(chargeType.getVatComputation());
 
cae.setEntries(entryService.findBySource(CmsConstants.CHARGE_TYPE_SOURCE , chargeType.getId()));
 
data.add(cae);
}
 
 
Map<String, Object> param = new HashMap<String, Object>();
param.put("Title", getString("report.title.chargetypelist"));
 
List<KeyValuePair> subreportKvp =  new ArrayList<KeyValuePair>();
subreportKvp.add(new KeyValuePair(0, "charge_type_entries.jrxml"));
 
try {
CmsUtil.showJasperReport(getRequestCycle(), data, "charge_type_list.jrxml", param, subreportKvp);
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
 
add(form);
form.add(code);
form.add(description);
form.add(includeInactive);
form.add(submit);
}
}
 

Show report:

public static void showJasperReport(RequestCycle requestCycle, List<?> data, String jrxmlFileName, Map parameters, List<KeyValuePair> subreports) throws JRException {
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(data);
 
CmsJasperReportsResource jr;
 
//Create an instance of JasperReport of the subreport then pass that instance to the main report as a parameter
if(parameters != null) {
putTitleSubreportInParam(parameters);
 
if(subreports != null) {
putSubreportsInParam(parameters, subreports);
}
} else {
parameters = new HashMap();
putTitleSubreportInParam(parameters);
}
 
InputStream jrxmlUrl = CmsJasperReportsResource.class.getResourceAsStream(jrxmlFileName);
JasperDesign jasperDesign = JRXmlLoader.load(jrxmlUrl);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); //produces a .jasper file when compiled using ANT
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource); //produces a jrprint file when compiled using ANT
jr = new CmsJasperReportsResource(jasperPrint);
requestCycle.scheduleRequestHandlerAfterCurrent(new ResourceRequestHandler(jr, null));
}
 
Main report:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.3.final using JasperReports Library version 6.0.3  -->
<!-- 2015-07-03T18:15:40 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Charge_Type_Report" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f2da39e4-6b1d-46bd-a7de-d8c092a8d640">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="TitleSubreport" class="net.sf.jasperreports.engine.JasperReport"/>
<parameter name="Logo" class="java.lang.String"/>
<parameter name="Title" class="java.lang.String"/>
<parameter name="0" class="net.sf.jasperreports.engine.JasperReport"/>
<queryString language="SQL">
<![CDATA[]]>
</queryString>
<field name="code" class="java.lang.String">
<fieldDescription><![CDATA[code]]></fieldDescription>
</field>
<field name="description" class="java.lang.String">
<fieldDescription><![CDATA[description]]></fieldDescription>
</field>
<field name="status" class="java.lang.Integer">
<fieldDescription><![CDATA[status]]></fieldDescription>
</field>
<field name="entries" class="java.util.List">
<fieldDescription><![CDATA[entries]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="111" splitType="Stretch">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<subreport>
<reportElement x="0" y="0" width="555" height="75" uuid="a7e88471-87ab-4fde-af5c-dd9efb3ba01d">
<property name="local_mesure_unitx" value="pixel"/>
<property name="com.jaspersoft.studio.unit.x" value="px"/>
<property name="local_mesure_unitwidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<subreportParameter name="Logo">
<subreportParameterExpression><![CDATA[$P{Logo}]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA[$P{TitleSubreport}]]></subreportExpression>
</subreport>
<textField>
<reportElement x="0" y="80" width="555" height="30" uuid="77214317-16c6-4274-ba47-a6894e398e7c">
<property name="local_mesure_unitx" value="pixel"/>
<property name="com.jaspersoft.studio.unit.x" value="px"/>
<property name="local_mesure_unitwidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<textElement textAlignment="Center">
<font size="16" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{Title}]]></textFieldExpression>
</textField>
</band>
</title>
<columnHeader>
<band height="20" splitType="Stretch">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<textField>
<reportElement mode="Transparent" x="0" y="0" width="110" height="20" backcolor="#80FFFF" uuid="8c18596e-045e-461d-ab0a-ea1a31aec7be">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<property name="local_mesure_unitwidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
</reportElement>
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Code"]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Transparent" x="110" y="0" width="370" height="20" backcolor="#80FFFF" uuid="599bd22c-bbb7-4397-b926-c8c17456fcfc">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<property name="local_mesure_unitwidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="local_mesure_unitx" value="pixel"/>
<property name="com.jaspersoft.studio.unit.x" value="px"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Description"]]></textFieldExpression>
</textField>
<rectangle>
<reportElement mode="Transparent" x="0" y="-1" width="555" height="20" uuid="d302a302-e5ae-4c65-868f-e5e8497932f7"/>
<graphicElement>
<pen lineWidth="0.5"/>
</graphicElement>
</rectangle>
<textField>
<reportElement mode="Transparent" x="445" y="0" width="110" height="20" backcolor="#80FFFF" uuid="10093ca2-491a-4db5-838f-44faebe163a5">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<property name="local_mesure_unitwidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
</reportElement>
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Status"]]></textFieldExpression>
</textField>
</band>
</columnHeader>
<detail>
<band height="83" splitType="Stretch">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<textField>
<reportElement x="0" y="0" width="110" height="15" uuid="2a7e1f46-89ee-4276-95b5-c54c85b9fa6e">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{code}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="110" y="0" width="334" height="16" uuid="c9f46d33-da4e-44a5-b6fc-cc5e12b16611">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<textFieldExpression><![CDATA[$F{description}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="445" y="0" width="110" height="15" uuid="e7b1b8a3-d7bc-432d-b486-11b3daa53087">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<property name="local_mesure_unity" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="px"/>
</reportElement>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{status} == 1 ? "Active" : "Inactive"]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="0" y="16" width="555" height="54" uuid="ae8044f8-7102-4fac-8d74-32269b39f994"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{entries})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{0}]]></subreportExpression>
</subreport>
</band>
</detail>
<pageFooter>
<band height="15" splitType="Stretch">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<textField>
<reportElement x="485" y="0" width="70" height="15" uuid="2e1789d0-d635-468d-8c6f-9d4da77c0c36">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
<line direction="BottomUp">
<reportElement x="0" y="0" width="555" height="1" uuid="9392f64f-f2d5-4416-9b38-586bf4754089">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
</line>
</band>
</pageFooter>
<summary>
<band height="42" splitType="Stretch">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</band>
</summary>
</jasperReport>
 
 
Subreport:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.3.final using JasperReports Library version 6.0.3  -->
<!-- 2015-07-03T18:15:38 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Charge_Type_Entries_Report" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="0aa70a7e-5b74-40b6-9f19-7a63b1d04123">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<queryString language="SQL">
<![CDATA[]]>
</queryString>
<field name="amount" class="java.math.BigDecimal">
<fieldDescription><![CDATA[amount]]></fieldDescription>
</field>
<field name="description" class="com.cmsbeta.hibernate.domain.EntryType">
<fieldDescription><![CDATA[entryType.description]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="57" splitType="Stretch">
<staticText>
<reportElement x="0" y="39" width="500" height="18" uuid="90bcd75e-481e-42f5-a187-4896b1e9578e"/>
<textElement>
<font size="11" isBold="true"/>
</textElement>
<text><![CDATA[Entry Description]]></text>
</staticText>
<staticText>
<reportElement x="500" y="39" width="95" height="18" uuid="ff86ec03-5cd8-4983-a8c1-afd6d11e7dae">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<property name="local_mesure_unity" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="px"/>
</reportElement>
<textElement>
<font size="11" isBold="true"/>
</textElement>
<text><![CDATA[Amount]]></text>
</staticText>
<staticText>
<reportElement x="0" y="4" width="595" height="30" uuid="de23d538-747a-4021-a8ed-7b744ba9beb3"/>
<textElement>
<font size="14" isBold="true"/>
</textElement>
<text><![CDATA[List of Entries]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="28" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="500" height="20" uuid="4fe8bf65-eb79-4bfc-8474-3883c371f184"/>
<textFieldExpression><![CDATA[$F{description}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="500" y="0" width="95" height="20" uuid="3d70aa01-bb72-4226-b672-17a01821891e"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{amount}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
 

 

sabmoonie's picture
Joined: Jul 3 2015 - 3:02am
Last seen: 11 months 1 week ago

just kindly show me in detail how to use java beans as datasource. could be like one datasource per subreport. thanks a lot

sabmoonie - 4 years 1 month ago

4 Answers:

0

1. Question: (Subreport using java beans)
jrxml: <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression> ???
I am don't see expression like 'new JRBeanCollecitionDataSource(collection)' for subreport.
2. Why subreport placed in title of main report? If you want display Entry in subreport then subreport must be placed in detail band whith dataSousrceExpression like new JRBeanCollectionDataSource($F{entries})
 

 

sanbez's picture
834
Joined: Jan 11 2011 - 2:06am
Last seen: 1 week 2 days ago
0

Hi Sanbez!

Thanks for the reply.

I have two subreports. One is on the title. It contains the company name and logo. And I dont have any problem with it. The one on the Detail band is the one which is not shown.

<subreport>
<reportElement x="0" y="16" width="555" height="54" uuid="ae8044f8-7102-4fac-8d74-32269b39f994"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{entries})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{0}]]></subreportExpression>
</subreport>
sabmoonie's picture
Joined: Jul 3 2015 - 3:02am
Last seen: 11 months 1 week ago
0

1. Insert static text in title band of Charge_Type_Entries_Report (subreport). Is it will displaing?
2. Show values $P{Logo} and $P{0}. You can display it in main report.
 

sanbez's picture
834
Joined: Jan 11 2011 - 2:06am
Last seen: 1 week 2 days ago
0

Hi Sanbez!

No but i already found out the answer.

 

To use a list as subreport/table/list element datasource,

  • make sure to create a JRBeanCollectionDataSource first out of the list
  • add on parameters
  • on the report, select list
  • go to Dataset properties(Tab)
  • select Dont use any connection or Data Source
  • add Parameter (Name: REPORT_DATA_SOURCE  and Expression: select the list data source param)

or if the list or set is already a part of the object used as the report's datasource,

  • on the report, select list
  • go to Dataset properties(Tab)
  • select Dont use any connection or Data Source
  • add Parameter (Name: REPORT_DATA_SOURCE  and Expression: "new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(param)")

hope this helps other jasper report users. ^^

 

 

 

 

sabmoonie's picture
Joined: Jul 3 2015 - 3:02am
Last seen: 11 months 1 week ago
Feedback