Docs are outdated and not helpful anyway. I use the dialog to add the class and and the static method, as well as the path to the .jar file holding the relevant classes.
When I hit test connection I get an error saying it cannot find the class ....
Yes the jar file is at that path. Do I need to further at that path somewhere else in the project properties or something??
Here's a link to the part of the docs that is supposed to describe this process
I think your problem in full name of class - probably the package is missing in your case.
Here is the sample how it works in Jaspersoft Studio 6.2.1 (JSS).
Bean Order:
package ru.alex;
public class Order {
private double price;
private int quantity;
private Product product;
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return this.quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public Product getProduct() {
return this.product;
}
public void setProduct(Product product) {
this.product = product;
}
public Order(double price, int quantity, Product product) {
this.price = price;
this.quantity = quantity;
this.product = product;
}
}
Bean Product:
package ru.alex;
public class Product {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Product(String name) {
this.name = name;
}
}
And the factory for getting Collection of Order objects with static method:
package ru.alex;
import java.util.*;
public class OrderFactory {
public static Collection<Order> getOrders() {
List<Order> orders = new ArrayList<>();
orders.add(new Order(8.0, 2, new Product("apples")));
orders.add(new Order(2.5, 10, new Product("oranges")));
return orders;
}
}
All classes are at ru.alex package.
The settings of Collection of JavaBeans type data adapter in JSS:
This data adapter was created with help of wizard:
I did not add the beans.jar to the project's Java Build Path in JSS and everything (adapter) works ok. It can be checked by pressing of Test button.
The checkbox Use field description does not play any role in this game.
I used the full class name: ru.alex.OrderFactory
in settings.
And now this adapter can be used in report.
Since the adapter is ready we can use it.
At Dataset and Query Dailog we can ignore message that class not found by ....
and manually add the fields after setting the class name.
The report will be something like this:
<jasperReport ...>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
<field name="price" class="java.lang.Double"/>
If we add the jar with our beans to the IDE build path, like this:
the behaviour will be changed. The list of fields will be appear automatically after typing the class name at Dataset and Query Dailog:
After adding second jar we can got the ClassCastException trouble. Only single jar with same classes should be added to classpath (JSS). Please look at the bottom of a post to find more information.
If we want to show only field from Order class we can use only the Dataset and Query Dailog to construct fields list.
The jrxml for showing prices & quantities of orders will be:
<?xml version="1.0" encoding="UTF-8"?>
<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="Report with Bean" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
<field name="product" class="ru.alex.Product">
<fieldDescription><![CDATA[product]]></fieldDescription>
</field>
<field name="quantity" class="java.lang.Integer">
<fieldDescription><![CDATA[quantity]]></fieldDescription>
</field>
<field name="price" class="java.lang.Double">
<fieldDescription><![CDATA[price]]></fieldDescription>
</field>
<detail>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="10" y="0" width="100" height="30"/>
<textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="110" y="0" width="100" height="30"/>
<textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
If we want to show, for example the product name we need to add the new field:
<field name="productName" class="java.lang.String">
<fieldDescription><![CDATA[product.name]]></fieldDescription>
</field>
In this case the template be:
<?xml version="1.0" encoding="UTF-8"?>
<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="Report with Bean" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
<field name="product" class="ru.alex.Product">
<fieldDescription><![CDATA[product]]></fieldDescription>
</field>
<field name="quantity" class="java.lang.Integer">
<fieldDescription><![CDATA[quantity]]></fieldDescription>
</field>
<field name="price" class="java.lang.Double">
<fieldDescription><![CDATA[price]]></fieldDescription>
</field>
<field name="productName" class="java.lang.String">
<fieldDescription><![CDATA[product.name]]></fieldDescription>
</field>
<detail>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="10" y="0" width="100" height="30"/>
<textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="110" y="0" width="100" height="30"/>
<textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="210" y="0" width="100" height="30"/>
<textFieldExpression><![CDATA[$F{productName}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Beware! We can face the issue described at Why do I get error when trying to retrive bean from my data adapter? post. We should stay only one jar with Bean classes. For example, jar at Java Build Path.
The full desciption is at referenced post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With