Pregunta

lets say i have a table test with columns id and name

on my bean i got this query

public List getTestList(){
  Query q = em.createNativeQuery("select * from test");
  List list = q.getResultList();
  return list;
}

and on my jsf page i have:

<ul>
    <ui:repeat id="resulta" value="#{testController.testList}" var="item"> 
        <li>#{item.id}</li>
    </ui:repeat>
</ul>

why do i get a SEVERE: javax.el.ELException: /test.xhtml: For input string: "id"

¿Fue útil?

Solución

why do i get a SEVERE: javax.el.ELException: /test.xhtml: For input string: "id"

Because a native query returns a List<Object[]>, not a List<Test>. You're basically attempting to access an Object[] array using a string such as "id" as index instead of an integer such as 0. If you look closer to the stack trace, then you should have noticed the presence of ArrayELResolver a little further in the stack after the exception, which should already have hinted that #{item} is actually been interpreted as an array.

So, if you absolutely can't obtain it as a fullworthy List<Test> (you can easily do inner joins using @ManyToOne and so on), then this should do to obtain the first column from the SELECT query:

<li>#{item[0]}</li>

Otros consejos

The SELECT clause queries more than one column or entity, the results are aggregated in an object array (Object[]) in the java.util.List returned by getResultList().

In the first place, native query isn't required in your case. The result of native query returns a list of object array. You have to create JPQL query instead of native query.

Query q = em.createQuery("select t from Test t", Test.class);
List<Test> list = q.getResultList();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top