尝试从数据适配器中检索bean时,为什么会出现错误(ClassCastException)? [英] Why do I get error (ClassCastException) when trying to retrive bean from my data adapter?

查看:147
本文介绍了尝试从数据适配器中检索bean时,为什么会出现错误(ClassCastException)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JasperSoft Studio创建了一个报告。



我正在参考这篇有用的帖子



详情:

  net.sf.jasperreports.engine.fill.JRExpressionEvalException:评估表达式时出错源文本:$ F {product} .getName()
at com.jaspersoft.studio.editor.preview.view.control.ReportControler.fillReport(ReportControler.java:530)
at com.jaspersoft。 studio.editor.preview.view.control.ReportControler.access $ 20(ReportControler.java:505)
...
引起:java.lang.ClassCastException:ru.alex.Product无法强制转换为ru.alex.Product
at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:277)
... 14更多

文件 sample.jrxml 是:

 <?xml version =1.0encoding =UTF-8?> 
<! - 使用JasperReports Library版本6.3.1与Jaspersoft Studio版本6.3.1.final创建 - >
<! - 2017-05-18T13:29:14 - >
< jasperReport xmlns =http://jasperreports.sourceforge.net/jasperreportsxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http: //jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsdname =samplepageWidth =595pageHeight =842columnWidth =555leftMargin =20 rightMargin =20topMargin =20bottomMargin =20uuid =ade9e357-e2d0-42bb-ae0d-000b69f4e2e9>
< property name =com.jaspersoft.studio.data.defaultdataadaptervalue =JavaBeanCollection - orders/>
< queryString>
<![CDATA []]>
< / queryString>
< field name =productclass =ru.alex.Product>
< fieldDescription><![CDATA [product]]>< / fieldDescription>
< / field>
< field name =quantityclass =java.lang.Integer>
< fieldDescription><![CDATA [数量]]>< / fieldDescription>
< / field>
< field name =priceclass =java.lang.Double>
< fieldDescription><![CDATA [price]]>< / fieldDescription>
< / field>
< title>
< band height =80splitType =Stretch>
< staticText>
< reportElement x =70y =49width =100height =30uuid =a19f5b7c-21ed-4c00-a224-af5cf7ef27ec/>
< text><![CDATA [price]]>< / text>
< / staticText>
< staticText>
< reportElement x =170y =49width =100height =30uuid =772c4807-25f7-4e7a-8a10-eba5232b92c7/>
< text><![CDATA [数量]]>< / text>
< / staticText>
< staticText>
< reportElement x =270y =49width =140height =30uuid =613da9ef-6a5a-45b2-8c8f-c3cd450e66ed/>
< text><![CDATA [product]]>< / text>
< / staticText>
< / band>
< / title>
< detail>
< band height =130splitType =Stretch>
< textField>
< reportElement x =70y =0width =100height =30uuid =a594224b-c015-4dab-b52b-6e317e76cea3/>
< textFieldExpression><![CDATA [$ F {price}]]>< / textFieldExpression>
< / textField>
< textField>
< reportElement x =170y =0width =100height =30uuid =b60503ca-f6bc-48dc-ad01-178d9befd805/>
< textFieldExpression><![CDATA [$ F {quantity}]]>< / textFieldExpression>
< / textField>
< textField>
< reportElement x =270y =0width =140height =30uuid =480bfb2f-2831-4700-8adc-f818bbbf6592/>
< textFieldExpression><![CDATA [$ F {product} .getName()]]>< / textFieldExpression>
< / textField>
< / band>
< / detail>
< / jasperReport>

JRBeanCollection 如下:

  public class MyImplementation实现JRDataSource {
//(...)
public static Collection< Order> getOrders(){
List< Order> orders = new ArrayList< Order>();
orders.add(新订单(1,aa,新BigDecimal(1111.11),2,新产品(苹果)));
orders.add(新订单(2,bb,新BigDecimal(2222.22),10,新产品(橙子)));

退货订单;
}
//(...)
}

你能否告诉我异常的原因是什么?

解决方案

它看起来像是 Jaspersoft Studio的一个bug / em>( JSS )。



我认为得到 ClassCastException 的原因(看看这部分的栈跟踪:引起:java.lang .ClassCastException:ru.alex.Product无法强制转换为ru.alex.Product )正在使用两个 jar 文件:




  • 为数据提供者设置了第一个 jar ;

  • 第二个 - 通过项目的构建路径。



是的,它是相同类的 jar 文件(物理上)。看起来在类路径中有多个 jar 的问题。



在理解了问题的根源后,我们可以很容易地解决这个问题。



我们应该只使用我们的bean类保留一个 jar - 在 JSS 构建路径:
< a href =https://i.stack.imgur.com/2PgoR.png =nofollow noreferrer>



这意味着我们应该从数据适配器的属性中删除 jar 。像这样:





此后最好是重新启动 JSS



对于此 jrxml

 <?xml version =1.0encoding =UTF-8?> 
< jasperReport xmlns =http://jasperreports.sourceforge.net/jasperreportsxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http: //jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsdname =用Bean报告pageWidth =595pageHeight =842columnWidth =555leftMargin = 20rightMargin =20topMargin =20bottomMargin =20>
< property name =com.jaspersoft.studio.data.defaultdataadaptervalue =JavaBeanCollection - orders/>
< field name =productclass =ru.alex.Product>
< fieldDescription><![CDATA [product]]>< / fieldDescription>
< / field>
< field name =quantityclass =java.lang.Integer>
< fieldDescription><![CDATA [数量]]>< / fieldDescription>
< / field>
< field name =priceclass =java.lang.Double>
< fieldDescription><![CDATA [price]]>< / fieldDescription>
< / field>
< detail>
< band height =30splitType =Stretch>
< textField>
< reportElement x =10y =0width =100height =30/>
< textFieldExpression><![CDATA [$ F {quantity}]]>< / textFieldExpression>
< / textField>
< textField>
< reportElement x =110y =0width =100height =30/>
< textFieldExpression><![CDATA [$ F {price}]]>< / textFieldExpression>
< / textField>
< textField>
< reportElement x =210y =0width =100height =30/>
< textFieldExpression><![CDATA [$ F {product} .getName()]]>< / textFieldExpression>
< / textField>
< / band>
< / detail>
< / jasperReport>

我们正在 JSS 成功获得报告:








一切正常(在 Java 项目中没有任何黑魔法,因为我们在classpath只有一个 jar 和我们的bean类。


I created a report using JasperSoft Studio.

I'm referencing to this helpful post Jaspersoft Studio: How to use Collection of Java Beans in data adapter by Alex K

I'd like to retrieve a list of orders. The class Order is defined as in the post:

public class Order {
    private double price;
    private int quantity;
    private Product product;

    // Getters & Setters
}

The report is generated with success if I'd like to retrieve price or quantity. But, once I retrieve product this is displayed:

Details:

net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: $F{product}.getName()
    at com.jaspersoft.studio.editor.preview.view.control.ReportControler.fillReport(ReportControler.java:530)
    at com.jaspersoft.studio.editor.preview.view.control.ReportControler.access$20(ReportControler.java:505)
...
Caused by: java.lang.ClassCastException: ru.alex.Product cannot be cast to ru.alex.Product
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:277)
    ... 14 more

The file sample.jrxml was:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-05-18T13:29:14 -->
<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="sample" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ade9e357-e2d0-42bb-ae0d-000b69f4e2e9">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <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>
    <title>
        <band height="80" splitType="Stretch">
            <staticText>
                <reportElement x="70" y="49" width="100" height="30" uuid="a19f5b7c-21ed-4c00-a224-af5cf7ef27ec"/>
                <text><![CDATA[price]]></text>
            </staticText>
            <staticText>
                <reportElement x="170" y="49" width="100" height="30" uuid="772c4807-25f7-4e7a-8a10-eba5232b92c7"/>
                <text><![CDATA[quantity]]></text>
            </staticText>
            <staticText>
                <reportElement x="270" y="49" width="140" height="30" uuid="613da9ef-6a5a-45b2-8c8f-c3cd450e66ed"/>
                <text><![CDATA[product]]></text>
            </staticText>
        </band>
    </title>
    <detail>
        <band height="130" splitType="Stretch">
            <textField>
                <reportElement x="70" y="0" width="100" height="30" uuid="a594224b-c015-4dab-b52b-6e317e76cea3"/>
                <textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="170" y="0" width="100" height="30" uuid="b60503ca-f6bc-48dc-ad01-178d9befd805"/>
                <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="270" y="0" width="140" height="30" uuid="480bfb2f-2831-4700-8adc-f818bbbf6592"/>
                <textFieldExpression><![CDATA[$F{product}.getName()]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

The JRBeanCollection is the following:

public class MyImplementation implements JRDataSource {
    // (...)
    public static Collection<Order> getOrders() {
        List<Order> orders = new ArrayList<Order>();
        orders.add(new Order(1, "aa", new BigDecimal("1111.11"), 2, new Product("apples")));
        orders.add(new Order(2, "bb", new BigDecimal("2222.22"), 10, new Product("oranges")));

        return orders;
        }
    // (...)
}

Could you please tell me what is the reason for the exception?

解决方案

It looks like a bug of Jaspersoft Studio (JSS).

I think that the reason of getting ClassCastException (look at this part of stacktrace: Caused by: java.lang.ClassCastException: ru.alex.Product cannot be cast to ru.alex.Product) is using of two jar files:

  • the first jar was set for a data provider;
  • second one - via project's build path.

Yes, it is the same jar file (physically) with the same classes. Looks like the issue of having multiple jar at classpath.

After understanding the root of the problem we can fix this issue very easy.

We should stay only one jar with our bean classes - at JSS build path:

It means that we should remove the jar from data adapter's properties. Like this:

After this it is better to restart JSS.

For this jrxml:

<?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>
            <textField>
                <reportElement x="210" y="0" width="100" height="30"/>
                <textFieldExpression><![CDATA[$F{product}.getName()]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

we are getting report at JSS successfully:


Everything working fine (without any black magic) at Java project, because we have only single jar with our bean classes at classpath.

这篇关于尝试从数据适配器中检索bean时,为什么会出现错误(ClassCastException)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆