Showing Hibernate/JPA results in JSF datatable cau

2019-01-03 07:19发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-01-03 07:37

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.

查看更多
淡お忘
3楼-- · 2019-01-03 07:51

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:

查看更多
登录 后发表回答