0

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"

galao
  • 1,281
  • 8
  • 26
  • 50

2 Answers2

0

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();
Nayan Wadekar
  • 11,444
  • 4
  • 50
  • 73
  • hi, actually i need it to be nativeQuery, because i need something like `"select t.*,nt.name from test as t Inner Join newTest as nt"` – galao May 03 '13 at 06:02
  • 2
    @galao Try to describe your actual problem, it misleads people, wasting time on both the sides. Similar one, can refer here - http://stackoverflow.com/a/4536802/366964 – Nayan Wadekar May 03 '13 at 06:09
0

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555