英文:
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("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.
<b>Native Query :</b>
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 <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 "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]
答案1
得分: 1
你的查询是 select c.*, p.* from ...
,因此Hibernate返回一个包含2元素数组的列表:第一个元素是Client
,第二个是Product
。
尽管它编译通过,但查询的结果不是List<Client>
。
在将其发送到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<Client>
.
You need to convert that list of arrays into a list of Client
before sending it to Jasperreports
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论