createNativeQuery返回对象

时间:2015-03-17 08:25:41

标签: mysql jsf jpa primefaces

我使用nativequery来获取行号。但是这会向我返回一个Object数组。这给了我一个:

SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/BowlingInfo] threw exception [For input string: "position"] with root cause
java.lang.NumberFormatException: For input string: "position"

在普通createQuery的情况下,它会正确地向我返回“Hallmaster”类,它将在我的primefaces xhtml中正确执行。

listHallmastaren = (List<Hallmaster>) em.createNativeQuery("SELECT  id, @rownum := @rownum + 1 AS position, e.name, e.score FROM HALLMASTER e, (SELECT @rownum:=0) t ORDER BY e.score desc").getResultList();
listHallmastaren = em.createQuery("select e from Hallmaster e").getResultList();

要正确地将本机查询强制回到您期望的类,您需要告诉查询返回该类的类型。请注意最后的Hallmaster.class。

listHallmastaren = (List<Hallmaster>) em.createNativeQuery("SELECT  id, @rownum := @rownum + 1 AS position, " +
        " e.name, e.score FROM HALLMASTER e, (SELECT @rownum:=0) t ORDER BY e.score desc", Hallmaster.class).getResultList();

0 个答案:

没有答案