EntityManager.createNativeQuery select count(*)

867 views
Skip to first unread message

Slack

unread,
Sep 2, 2010, 10:51:56 AM9/2/10
to jav...@googlegroups.com
Bom dia,

Estou tentando fazer o seguinte:

Query q = em.createNativeQuery(" select count(*) from tabela ", Integer.class);
return (Integer) q.getSingleResult();

mas dá o erro:
Caused by: Exception [EclipseLink-6007] (Eclipse Persistence Services - 1.0.1 (Build 20080905)): org.eclipse.persistence.exceptions.QueryException
Exception Description: Missing descriptor for [class java.lang.Integer].
Query: ReadAllQuery(java.lang.Integer)

Como proceder nesse caso?

Obrigado.

Flávio Cysne Ferreira Gomes

unread,
Sep 2, 2010, 11:06:31 AM9/2/10
to jav...@googlegroups.com
Slack,

    é realmente necessário usar uma native query? tente trocar o count(*) por um count(1) ou count(id) e dê a ele um alias (exemplo: count(id) as qtd). Se não me engano o retorno do count é um Long e não um Integer. Se usar hql/jpql também for uma opção sugiro a leitura do link abaixo.

http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html/queryhql.html#queryhql-select

Espero ter ajudado.

    Flávio Cysne

--
Você recebeu esta mensagem por que é membro do Google Group "Javasf".
http://groups.google.com/group/javasf
 
Conheça também:
- Grupo Java Brazil em http://groups.google.com/group/thejavabrazil

Slack

unread,
Sep 2, 2010, 1:16:04 PM9/2/10
to jav...@googlegroups.com
deu certo..
valeu Flavio.


2010/9/2 Flávio Cysne Ferreira Gomes <flavi...@gmail.com>
Reply all
Reply to author
Forward
0 new messages