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

huangapple go评论62阅读模式
英文:

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

问题

以下是您要翻译的内容:

获取:

net.sf.jasperreports.engine.JRException: 从bean检索字段值时出错: fullname.

仅在使用带有JasperReports的Hibernate中的NativeQuery时出现此问题。
当我使用JPA条件查询时,就没有问题。

JPA条件查询

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

但是当我使用NativeQuery时,我遇到以下问题:
net.sf.jasperreports.engine.JRException: 从bean检索字段值时出错: fullname.

Native查询:

try (Session session = sessionFactory.openSession()) {
    List<Client> query = session.createNativeQuery("select c.*, p.*\n" +
            "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"/>
</jasperReport>

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

我已经从Jasper JRXML的字段中删除了<FieldDescription>。

我还尝试通过以下方式传递并从new JRBeanColllectionDataSource(collection, false);中删除了false参数:

new JRBeanColllectionDataSource(collection);

对于JPA条件查询,传递“false”参数只适用于Native查询无论是否传递参数,都会出现以下错误:

net.sf.jasperreports.engine.JRException: 从bean检索字段值时出错: fullname.

我正在尝试使用NativeQuery来提高JasperReport的速度,这是我之前问题的建议解决方案:

所以我想要针对这个问题或者我的之前的问题获得解决方案:

Jasper Reports 6.14填充和生成报表速度非常慢[closed]

英文:

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(&quot;id&quot;), 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.

<b>Native Query :</b>

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

Here Jasper JRXML

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;jasperReport xmlns=&quot;http://jasperreports.sourceforge.net/jasperreports&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd&quot; name=&quot;Quotation&quot; pageWidth=&quot;595&quot; pageHeight=&quot;600&quot; whenNoDataType=&quot;AllSectionsNoDetail&quot; columnWidth=&quot;555&quot; leftMargin=&quot;20&quot; rightMargin=&quot;20&quot; topMargin=&quot;20&quot; bottomMargin=&quot;20&quot; uuid=&quot;3954e5eb-656a-454e-b9e5-35f7e5262d48&quot;&gt;
    &lt;property name=&quot;com.jaspersoft.studio.data.defaultdataadapter&quot; value=&quot;One Empty Record&quot;/&gt;
    &lt;property name=&quot;ireport.zoom&quot; value=&quot;1.0&quot;/&gt;
    &lt;property name=&quot;ireport.x&quot; value=&quot;0&quot;/&gt;
    &lt;property name=&quot;ireport.y&quot; value=&quot;0&quot;/&gt;
    &lt;field name=&quot;fullname&quot; class=&quot;java.lang.String&quot;/&gt;
    &lt;field name=&quot;mobile&quot; class=&quot;java.lang.String&quot;/&gt;
    &lt;field name=&quot;city&quot; class=&quot;java.lang.String&quot;/&gt;
    &lt;field name=&quot;address&quot; class=&quot;java.lang.String&quot;/&gt;
    &lt;field name=&quot;quotationNo&quot; class=&quot;java.lang.String&quot;/&gt;
    &lt;field name=&quot;valid&quot; class=&quot;java.lang.String&quot;/&gt;
    &lt;field name=&quot;product.description&quot; class=&quot;java.lang.String&quot;/&gt;
    &lt;field name=&quot;product.cost&quot; class=&quot;java.lang.String&quot;/&gt;
    &lt;field name=&quot;product.name&quot; class=&quot;java.lang.String&quot;/&gt;

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 <FieldDescription> from fields in Jasper JRXML

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

new JRBeanColllectionDataSource(collection);  

Passing &quot;false&quot; 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]

答案1

得分: 1

你的查询是 select c.*, p.* from ...,因此Hibernate返回一个包含2元素数组的列表:第一个元素是Client,第二个是Product

尽管它编译通过,但查询的结果不是List&lt;Client&gt;

在将其发送到Jasperreports之前,你需要将这个数组列表转换为Client的列表。

英文:

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&lt;Client&gt;.

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

huangapple
  • 本文由 发表于 2020年9月6日 20:26:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63764122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定