net.sf.jasperreports.engine.jrexception:从bean:fullname检索字段值时出错使用nativequery时

kse8i1jr  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(331)

得到:

net.sf.jasperreports.engine.JRException: Error retrieving field value from bean: fullname.

仅在使用时 NativeQueryHibernateJasperReports .
当我使用jpa条件查询时,就没有问题了
jpa标准

try (Session session = sessionFactory.openSession()) {
    CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
    CriteriaQuery Client criteriaQuery = criteriaBuilder.createQuery(Client.class);
    Root root = criteriaQuery.from(Client.class);
    criteriaQuery.where(criteriaBuilder.equal(root.get("id"), id));
    return session.createQuery(criteriaQuery).getResultList();
}

但是当我使用nativequery时 net.sf.jasperreports.engine.JRException: Error retrieving field value from bean: fullname.本机查询:

try (Session session = sessionFactory.openSession()) {
    List<Client> query = session.createNativeQuery("select c.*, p.*
        from client c join product p on c.pid = p.id where c.id = :id")
        .addEntity("c", Client.class).addJoin("p", "c.product")
        .setParameter("id", id).list();
    return query;
}

这里是jasper 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="Quotation" pageWidth="595" pageHeight="600" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="3954e5eb-656a-454e-b9e5-35f7e5262d48">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <field name="fullname" class="java.lang.String"/>
    <field name="mobile" class="java.lang.String"/>
    <field name="city" class="java.lang.String"/>
    <field name="address" class="java.lang.String"/>
    <field name="quotationNo" class="java.lang.String"/>
    <field name="valid" class="java.lang.String"/>
    <field name="product.description" class="java.lang.String"/>
    <field name="product.cost" class="java.lang.String"/>
    <field name="product.name" class="java.lang.String"/>

客户端.java:

public class Client  implements java.io.Serializable {

 private Integer id;
 private Product product;
 private String fullname;
 private String business;
 private String address;
 private String city;
 private String mobile;
 private String addedBy;
 private String date;
 private String status;
 private String quotationNo;
 private String valid;
 private String productName;
 private Set orderses = new HashSet(0);

public String getFullname() {
    return this.fullname;
}

public void setFullname(String fullname) {
    this.fullname = fullname;
}

我已经从jasper jrxml中删除了字段
我还尝试在中传递和删除假参数 new JRBeanColllectionDataSource(collection, false); 如下所示,

new JRBeanColllectionDataSource(collection);

经过 "false" parameter 只适用于 JPA criteria 为了 Native query 参数是否被传递并不重要

net.sf.jasperreports.engine.JRException: Error retrieving field value from bean: fullname.

我在努力 NativeQuery 以提高 JasperReport 这是我之前的问题的建议
所以我想要的解决方案要么是这个问题,要么是我之前的问题
jasper reports 6.14填写和生成报告太慢[关闭]

wmtdaxz3

wmtdaxz31#

你的问题是 select c.*, p.* from ... 所以hibernate返回一个2元素数组的列表:第一个元素是 Client 第二个是 Product .
即使它编译了,查询的结果也不是 List<Client> .
您需要将数组列表转换为 Client 在发给jasperreports之前

相关问题