简体   繁体   中英

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

Getting:

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

Only when using NativeQuery in Hibernate with JasperReports .

When I use JPA criteria query then there's no issue

JPA Criteria

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();
}

But when I use NativeQuery I face
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;
}

Here 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"/>

Client.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;
}  

I've already removed from fields in Jasper JRXML

I also tried passing and removing false parameter in new JRBeanColllectionDataSource(collection, false); as follow,

new JRBeanColllectionDataSource(collection);  

Passing "false" parameter only works for JPA criteria for Native query it doesn't matter whether parameter is passed or not it gives

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

I'm trying NativeQuery to improve speed of JasperReport which is suggested for my previous question

So solution I want either for this one or for my previous question
Jasper Reports 6.14 filling and generating report is so slow [closed]

Your query is select c.*, p.* from ... so Hibernate returns a list of 2-elements arrays: the 1st element is the Client and the 2nd is the Product .

Even though it compiles, the result of the query is not a List<Client> .

You need to convert that list of arrays into a list of Client before sending it to Jasperreports

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM