3

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
kark
  • 4,763
  • 6
  • 30
  • 44

2 Answers2

7

Your Hibernate query is actually returning a List<Object[]>, not a List<Employee> as you incorrectly assumed during the unchecked cast.

Evidence is in the stack trace:

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)
    ...

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.

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.

    <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>
    
  • Fix the Hibernate query to return a real List<Employee>.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for your response, i understand that i mentioned that column name while retrieving record from DB,that's why it retrieved as `Object[]` right.. My problem solved by your First option..i mentioned `#{emp[0]}` alone insteadOf mentioning the column name( `#{emp.empname}`)...Thank you very much .. – kark Oct 31 '13 at 07:16
0

To return "a real List<Object>" instead of a List<Object[]>, as @BalusC says, you must specify the class of the resulting instances.

getEntityManager().createNativeQuery(
                "SELECT * FROM ...",
                 EntityUser.class
).getResultList();

Here it is: EntityUser.class.

Alexsoyes
  • 444
  • 4
  • 12