在 JSF 数据表中显示 Hibernate/JPA 结果原因:java.lang.NumberFormatException:对于输入字符串:“[propertyname]"; [英] Showing Hibernate/JPA results in JSF datatable causes: java.lang.NumberFormatException: For input string: "[propertyname]"
问题描述
我最初使用以下查询来获取List
:
I've initially the below query to obtain List<Employee>
:
Query query = session.createQuery("select table1 from Table as table1");
this.employees = (List<Employee>) query.list();
这在下面的数据表中成功呈现:
This is successfully rendered in below datatable:
<p:dataTable var="employee" value="#{bean.employees}">
<p:column id="name" headerText="Name">
<h:outputText value="#{employee.name}" />
</p:column>
<p:column id="id" headerText="ID" >
<h:outputText value="#{employee.id}" />
</p:column>
</p:dataTable>
但是,当我尝试从以下 2 个表中检索它时:
However, when I try to retrieve it from 2 tables as below:
Query query = session.createQuery("select a.name, b.id from Table1 as a, Table2 as b");
this.employees = (List<Employee>) query.list();
它抛出以下异常:
java.lang.NumberFormatException: For input string: "name"
at java.lang.NumberFormatException.forInputString(Unknown source)
at java.lang.Integer.parseInt(Unknown source)
at java.lang.Integer.parseInt(Unknown source)
at javax.el.ArrayELResolver.toInteger(ArrayELResolver.java:166)
at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)
...
这是怎么引起的,我该如何解决?
How is this caused and how can I solve it?
推荐答案
您的 Hibernate 查询实际上返回的是 List
,而不是 >列出<员工>
,正如您在未经检查的演员期间错误地假设的那样.
Your Hibernate query is actually returning a List<Object[]>
, not a List<Employee>
as you incorrectly assumed during the unchecked cast.
证据在堆栈跟踪中:
java.lang.NumberFormatException: For input string: "name"
at java.lang.NumberFormatException.forInputString(Unknown source)
at java.lang.Integer.parseInt(Unknown source)
at java.lang.Integer.parseInt(Unknown source)
at javax.el.ArrayELResolver.toInteger(ArrayELResolver.java:166)
at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)
...
ArrayELResolver
仅涉及name
的基础(因此,#{employee}
)表示像 Object[]
这样的数组,而不是像 Bean
这样的 javabean.EL 正在尝试使用 name
通过索引获取数组项,对于第一项,它只能是一个整数,例如 #{employee[0]}
.但是,字符串值 "name"
不能解析为整数,因此出现此异常.
The ArrayELResolver
is only involved when the base of name
(thus, the #{employee}
) represents an array like Object[]
not a javabean like Bean
. EL is attempting to use the name
to obtain the array item by index, which can only be an integer like so #{employee[0]}
for the 1st item. However, the string value "name"
is not parseable as an integer and hence this exception.
您有 2 个选项可以解决此问题:
You have 2 options to solve this problem:
更改 JSF 代码以期待
List
.使用例如#{employee[0]}
、#{employee[1]}
等
<p:dataTable var="employee" value="#{bean.employees}">
<p:column id="name" headerText="Name">
<h:outputText value="#{employee[0]}" />
</p:column>
<p:column id="id" headerText="ID" >
<h:outputText value="#{employee[1]}" />
</p:column>
</p:dataTable>
修复 Hibernate 查询以返回 real List
.
这篇关于在 JSF 数据表中显示 Hibernate/JPA 结果原因:java.lang.NumberFormatException:对于输入字符串:“[propertyname]";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!