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?

2 Answers 2

8

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:

1
  • 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, 2013 at 7: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.