找不到 iReport JRValidationException 字段

iReport JRValidationException field not found

我正在尝试使用 JasperReports 和 iReport 迈出第一步。最近我解决了 中的数据库连接问题。之后,我创建了另一个与 MySQL 数据库的成功连接,只是为了巩固知识;我进行了查询,但现在我收到另一个错误 - 我想 - 与查询本身或类似的东西有关。

情况

当我尝试创建一个报告查询时,字段(table 列)被检索并且它的类型被正确识别但是数据(行)给我一个错误消息

Error: net.sf.jasperreports.engine.JRException: Error excecuting SQL statement for: data

错误如下图所示:

然后,当我尝试使用查询字段预览报告时,出现以下错误:

net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 
    1. Field not found : idaddress
    2. Field not found : city
  at net.sf.jasperreports.engine.design.JRAbstractCompiler.verifyDesign(JRAbstractCompiler.java:271)
  at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:153)
  at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:354)
  at net.sf.jasperreports.engine.JasperCompileManager.compileToFile(JasperCompileManager.java:270)
  at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:563)
  at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:528)
  at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)
  at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)

jrxml 文件中,字段定义正确,正如我在 中看到的那样:

<subDataset name="MyDataset" uuid="b9d7b569-d19e-451e-8a15-c558918e1698">
    <queryString language="SQL">
        <![CDATA[SELECT
 address.`idaddress` AS address_idaddress,
 address.`city` AS address_city
 FROM
 `address` address]]>
    </queryString>
    <field name="address_idaddress" class="java.lang.Integer"/>
    <field name="address_city" class="java.lang.String"/>
</subDataset>

项目

完整的 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="PrimerReporte" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="eb8d4459-c8dc-4c96-bc35-ac988007e32a">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <style name="table">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TH" mode="Opaque" backcolor="#A52A2A">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_CH" mode="Opaque" backcolor="#FFBFBF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
        <conditionalStyle>
            <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
            <style backcolor="#FFEFEF"/>
        </conditionalStyle>
    </style>
    <subDataset name="MyDataset" uuid="b9d7b569-d19e-451e-8a15-c558918e1698">
        <queryString language="SQL">
            <![CDATA[SELECT
     address.`idaddress` AS address_idaddress,
     address.`city` AS address_city
FROM
     `address` address]]>
        </queryString>
        <field name="address_idaddress" class="java.lang.Integer"/>
        <field name="address_city" class="java.lang.String"/>
    </subDataset>
    <parameter name="idaddress" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <parameter name="city" class="java.lang.String">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT
     address.`idaddress` AS address_idaddress,
     address.`city` AS address_city
FROM
     `address` address]]>
    </queryString>
    <field name="address_idaddress" class="java.lang.Integer"/>
    <field name="address_city" class="java.lang.String"/>
    <filterExpression><![CDATA[$F{idaddress}]]></filterExpression>
    <group name="city">
        <groupExpression><![CDATA[$F{city}]]></groupExpression>
    </group>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="121" y="0" width="313" height="49" uuid="e3c5e2b7-14a4-4e26-a90c-ae4702f4ac65"/>
                <textElement>
                    <font size="36"/>
                </textElement>
                <textFieldExpression><![CDATA[Mi primer reporte]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch">
            <staticText>
                <reportElement x="22" y="0" width="510" height="35" uuid="7d375361-1b1d-4a7b-8788-9dd432b86c43"/>
                <textElement>
                    <font size="18"/>
                </textElement>
                <text><![CDATA[Este es un reporte de prueba para aprender a usar iReport]]></text>
            </staticText>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <componentElement>
                <reportElement key="table" style="table" x="0" y="0" width="401" height="80" uuid="8c775bc1-4076-44c4-a7af-1fc405283908"/>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="MyDataset" uuid="12ca7d6a-d625-42ab-bff9-57937d50f096">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource(1)]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="200" uuid="37b69ab0-be68-4dda-8739-08860a5dd8c7">
                        <jr:tableHeader style="table_TH" height="30" rowSpan="1"/>
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="200" height="30" uuid="91dcc02e-4f54-446e-9092-d8e8fef6bc78"/>
                                <textFieldExpression><![CDATA[$F{address_idaddress}]]></textFieldExpression>
                            </textField>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="20" rowSpan="1"/>
                    </jr:column>
                    <jr:column width="200" uuid="36dc21c0-a551-4df8-a180-95cf5b9cfc0c">
                        <jr:tableHeader style="table_TH" height="30" rowSpan="1"/>
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="200" height="30" uuid="294719c1-d37b-4b53-ab0e-2045397c0d60"/>
                                <textFieldExpression><![CDATA[$F{address_city}]]></textFieldExpression>
                            </textField>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="20" rowSpan="1"/>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

连接的配置是这样的:

注意:是的,服务器没有密码,只是测试环境...

一些尝试

我在同一个 post 中尝试了其他答案,但其中 none 对我有用。最后我尝试更改连接 URL 添加端口和用户,但错误仍然存​​在。

不知道为什么在 <subDataset> 元素下面我有以下 XML:

<queryString>
    <![CDATA[SELECT
 *
 FROM
 `address` address
 ORDER BY
 city ASC]]>
</queryString>
<field name="idaddress" class="java.lang.Integer">
    <fieldDescription><![CDATA[Address table ID]]></fieldDescription>
</field>
<field name="city" class="java.lang.String">
    <fieldDescription><![CDATA[The city]]></fieldDescription>
</field>
<group name="city">
    <groupExpression><![CDATA[$F{city}]]></groupExpression>
</group>

看起来与 <subDataset> 一样,但确实存在。我不想删除,因为它是由 iReport 生成的,所以我认为它是正确的。

所以问题是:我在这里缺少什么?我可以尝试解决这个问题吗?我在哪里可以找到一些线索?

提前感谢您的回答。

您对 Jasper Report 的工作原理、主数据源和子数据集之间的区别以及使用它们时需要引用与您正在使用的数据源相关的字段有一些困惑。

消除一些困惑:

  • 主数据源会执行,jasper report会迭代 (重复)每条记录的详细信息范围。

  • 子数据集是 another datasource,您可以将其传递给组件。

您的报告的解决方案是仅使用主查询(删除 jr:table 组件及其子数据集),或者如果您想使用 jr:table,有一个主数据源 returns 只有 1 条记录,然后将子数据源和 连接 传递给 jr:table 组件

在详细信息带中使用 textField 的示例

<?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="PrimerReporte" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="eb8d4459-c8dc-4c96-bc35-ac988007e32a">
    <queryString>
        <![CDATA[SELECT
     address.`idaddress` AS address_idaddress,
     address.`city` AS address_city
FROM
     `address` address]]>
    </queryString>
    <field name="address_idaddress" class="java.lang.Integer"/>
    <field name="address_city" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="121" y="0" width="313" height="49" uuid="e3c5e2b7-14a4-4e26-a90c-ae4702f4ac65"/>
                <textElement>
                    <font size="36"/>
                </textElement>
                <textFieldExpression><![CDATA[Mi primer reporte]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch">
            <staticText>
                <reportElement x="22" y="0" width="510" height="35" uuid="7d375361-1b1d-4a7b-8788-9dd432b86c43"/>
                <textElement>
                    <font size="18"/>
                </textElement>
                <text><![CDATA[Este es un reporte de prueba para aprender a usar iReport]]></text>
            </staticText>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="20" uuid="2604ec98-ab97-4192-9a22-c6c4f95b2416"/>
                <text><![CDATA[address_idaddress]]></text>
            </staticText>
            <staticText>
                <reportElement x="100" y="0" width="100" height="20" uuid="afb38cfb-3eeb-4af9-a520-93338b12eed9"/>
                <text><![CDATA[address_city]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20" uuid="aabd4a33-ecb9-4c60-b366-f770a01c32f8"/>
                <textFieldExpression><![CDATA[$F{address_idaddress}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="0" width="100" height="20" uuid="58913924-4ffe-4e78-a783-b2364f66c27a"/>
                <textFieldExpression><![CDATA[$F{address_city}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

Note: No more table component and subdatasource, the fields of the main datasource are referenced directly in the textField's. The detail band will be repeated as many time as there are records in your query.

使用 jr:table 组件的示例

<?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="PrimerReporte" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="eb8d4459-c8dc-4c96-bc35-ac988007e32a">
    <style name="table">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TH" mode="Opaque" backcolor="#A52A2A">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_CH" mode="Opaque" backcolor="#FFBFBF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
        <conditionalStyle>
            <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
            <style backcolor="#FFEFEF"/>
        </conditionalStyle>
    </style>
    <subDataset name="MyDataset" uuid="b9d7b569-d19e-451e-8a15-c558918e1698">
        <queryString language="SQL">
            <![CDATA[SELECT
     address.`idaddress` AS address_idaddress,
     address.`city` AS address_city
FROM
     `address` address]]>
        </queryString>
        <field name="address_idaddress" class="java.lang.Integer"/>
        <field name="address_city" class="java.lang.String"/>
    </subDataset>
    <queryString>
        <![CDATA[SELECT 1]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="121" y="0" width="313" height="49" uuid="e3c5e2b7-14a4-4e26-a90c-ae4702f4ac65"/>
                <textElement>
                    <font size="36"/>
                </textElement>
                <textFieldExpression><![CDATA[Mi primer reporte]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch">
            <staticText>
                <reportElement x="22" y="0" width="510" height="35" uuid="7d375361-1b1d-4a7b-8788-9dd432b86c43"/>
                <textElement>
                    <font size="18"/>
                </textElement>
                <text><![CDATA[Este es un reporte de prueba para aprender a usar iReport]]></text>
            </staticText>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <componentElement>
                <reportElement key="table" style="table" x="0" y="0" width="401" height="80" uuid="8c775bc1-4076-44c4-a7af-1fc405283908"/>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="MyDataset" uuid="ec302d39-8b71-4c05-86c0-74bb5a2b71d4">
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:column width="200" uuid="37b69ab0-be68-4dda-8739-08860a5dd8c7">
                        <jr:tableHeader style="table_TH" height="30" rowSpan="1"/>
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="200" height="30" uuid="91dcc02e-4f54-446e-9092-d8e8fef6bc78"/>
                                <textFieldExpression><![CDATA[$F{address_idaddress}]]></textFieldExpression>
                            </textField>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="20" rowSpan="1"/>
                    </jr:column>
                    <jr:column width="200" uuid="36dc21c0-a551-4df8-a180-95cf5b9cfc0c">
                        <jr:tableHeader style="table_TH" height="30" rowSpan="1"/>
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="200" height="30" uuid="294719c1-d37b-4b53-ab0e-2045397c0d60"/>
                                <textFieldExpression><![CDATA[$F{address_city}]]></textFieldExpression>
                            </textField>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="20" rowSpan="1"/>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

Note: How the main query is only SELECT 1, mysql statement that will return 1 (in 1 record) and how the $P{REPORT_CONNECTION} is passed so that the subDataset can execute the query.