首页 > 解决方案 > 如何使用 Spring Data Jpa 原生查询填充 JasperReports 报告?

问题描述

我正在尝试使用自定义的 sql qyery 填充 jasper 报告,但出现此错误:-

java.lang.NoSuchMethodException: Unknown property 'vehicle_class' on class 'class [Ljava.lang.Object;'
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1270) ~[commons-beanutils-1.9.4.jar:1.9.4]
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:809) ~[commons-beanutils-1.9.4.jar:1.9.4]
    at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:885) ~[commons-beanutils-1.9.4.jar:1.9.4]
    at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:464) ~[commons-beanutils-1.9.4.jar:1.9.4]
    at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:185) ~[jasperreports-6.17.0.jar:6.17.0-6d93193241dd8cc42629e188b94f9e0bc5722efd]
    at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:170) ~[jasperreports-6.17.0.jar:6.17.0-6d93193241dd8cc42629e188b94f9e0bc5722efd]

这是我的碧玉文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.17.0.final using JasperReports Library version 6.17.0-6d93193241dd8cc42629e188b94f9e0bc5722efd  -->
<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="ViolationClassAndAverageSpeed" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="dcaca94b-6e83-47dc-bd14-baca011b1845">
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <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>
    </style>
    <subDataset name="Dataset1" uuid="1f3f11d6-c688-4625-b387-2808efd481fc">
        <queryString language="SQL">
            <![CDATA[SELECT vehicle_class, COUNT(*)/SUM(1e0/speed) AS speed FROM violations GROUP BY vehicle_class]]>
        </queryString>
        <field name="vehicle_class" class="java.lang.String"/>
        <field name="speed" class="java.lang.Double"/>
        <group name="vehicle_class">
            <groupExpression><![CDATA[$F{vehicle_class}]]></groupExpression>
        </group>
    </subDataset>
    <queryString language="SQL">
        <![CDATA[SELECT vehicle_class, COUNT(*)/SUM(1e0/speed) AS speed FROM violations GROUP BY vehicle_class]]>
    </queryString>
    <field name="vehicle_class" class="java.lang.String"/>
    <field name="speed" class="java.lang.Double"/>
    <group name="vehicle_class">
        <groupExpression><![CDATA[$F{vehicle_class}]]></groupExpression>
    </group>
    <detail>
        <band height="349" splitType="Stretch">
            <componentElement>
                <reportElement x="10" y="10" width="200" height="200" uuid="5df1aded-bce1-4d9a-8c73-1950d7161a02">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                </reportElement>
                <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="Dataset1" uuid="a331d750-4a48-46e4-9f1c-cac4cfbf2e72">
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:column width="100" uuid="4545a2e7-6253-4848-a00b-063337993b35">
                        <jr:tableHeader style="Table_TH" height="30"/>
                        <jr:tableFooter style="Table_TH" height="30"/>
                        <jr:groupHeader groupName="vehicle_class">
                            <jr:cell style="Table_CH" height="30"/>
                        </jr:groupHeader>
                        <jr:groupFooter groupName="vehicle_class">
                            <jr:cell style="Table_CH" height="30"/>
                        </jr:groupFooter>
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="ec2d5ab9-d65e-4485-acbc-a41aad6097d8"/>
                                <text><![CDATA[vehicle_class]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table_CH" height="30"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="b46de1f3-0544-4e4b-a52e-0d46e5884010"/>
                                <textFieldExpression><![CDATA[$F{vehicle_class}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="100" uuid="fdb570b8-20de-42d9-ba5a-38f593535eb7">
                        <jr:tableHeader style="Table_TH" height="30"/>
                        <jr:tableFooter style="Table_TH" height="30"/>
                        <jr:groupHeader groupName="vehicle_class">
                            <jr:cell style="Table_CH" height="30"/>
                        </jr:groupHeader>
                        <jr:groupFooter groupName="vehicle_class">
                            <jr:cell style="Table_CH" height="30"/>
                        </jr:groupFooter>
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="2c555423-d793-45d4-96f0-e9603c6edac6"/>
                                <text><![CDATA[speed]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table_CH" height="30"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="f5539efd-3550-4a93-9df1-3e172ce72d6a"/>
                                <textFieldExpression><![CDATA[$F{speed}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

存储库

@Repository
public interface ViolationRepository extends JpaRepository<Violation, Long>{
    
    @Query(value = "SELECT vehicle_class, COUNT(*)/SUM(1e0/speed) AS avg_speed FROM violations GROUP BY vehicle_class", nativeQuery = true)
    List<Object> findCustomAll();
}

控制器类

@RequestMapping( "/pdf")
public void getReportsinPDF(HttpServletResponse response) throws JRException, IOException {
    InputStream jasperStream = (InputStream) this.getClass().getResourceAsStream("/ViolationClassAndAverageSpeed.jasper");
        
    // Adding attribute names
    Map<String, Object> params = new HashMap<>();
    
        
    // Fetching the student from the data database.
    final JRBeanCollectionDataSource source = new JRBeanCollectionDataSource(violationnService.getViolation());

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, source);

    response.setContentType("application/x-pdf");
    response.setHeader("Content-disposition", "inline; filename=ViolationList.pdf");

    final ServletOutputStream outStream = response.getOutputStream();
    JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
}

实体类

@Entity
@Table(name = "violations")
public class Violation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "violation_id")
    private long violation_id;

    @Column(name = "speed", length = 3)
    private Integer speed;

    @Column(name = "vehicle_class", length = 10)
    private String vehicle_class;

    public String getVehicle_class() {
        return vehicle_class;
    }

    public void setVehicle_class(String vehicle_class) {
        this.vehicle_class = vehicle_class;
    }

    public Integer getSpeed() {
        return speed;
    }

    public void setSpeed(Integer speed) {
        this.speed = speed;
    }
}    

服务等级:

@Service
public class ViolationService {

    @Autowired
    private ViolationRepository violationRepo;

    public List<Object> getViolation() {
        return violationRepo.findCustomAll();
    }
}

我正在使用 jdbc 适配器,我正在尝试在我的存储库中使用此查询填充 jasper 报告

SELECT vehicle_class, COUNT(*)/SUM(1e0/speed) AS speed FROM violations GROUP BY vehicle_class

我从报告中删除了字段描述,但仍然不值得。

标签: javaspring-mvcspring-data-jpajasper-reportsjavabeans

解决方案


推荐阅读