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>
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();
It throws the following exception:
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?
To return "a real
List<Object>
" instead of aList<Object[]>
, as @BalusC says, you must specify the class of the resulting instances.Here it is:
EntityUser.class
.Your Hibernate query is actually returning a
List<Object[]>
, not aList<Employee>
as you incorrectly assumed during the unchecked cast.Evidence is in the stack trace:
The
ArrayELResolver
is only involved when the base ofname
(thus, the#{employee}
) represents an array likeObject[]
not a javabean likeBean
. EL is attempting to use thename
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.You have 2 options to solve this problem:
Alter the JSF code to expect a
List<Object[]>
. Use e.g.#{employee[0]}
,#{employee[1]}
, etc.Fix the Hibernate query to return a real
List<Employee>
.See also: