JPA: createNativeQuery.getSingleResult () returns an object, how can I get the value of a single attribute inside this object

I have a request like this

SET @rownum := 0; SELECT rank, id, point FROM ( SELECT @rownum := @rownum + 1 AS rank, id, point FROM user ORDER BY point DESC ) AS result WHERE id = 0; 

Therefore, I use EntityManager#createNativeQuery to execute this query.

 Object temp = em.createNativeQuery(sql).getSingleResult(); //sql is the above SQL query 

So now the temp object contains information about rank , id and point . Please note: rank not an attribute inside my object, rank calculated when the request is executed → cannot pass this object to my Entity.

So how can I get the rank value?

EDIT
Here is the answer to what I am looking for. Therefore instead

 Object temp = em.createNativeQuery(sql).getSingleResult(); 

Do it

 Object[] temp = (Object [])em.createNativeQuery(sql).getSingleResult(); 

Since I want to know the rank value, I would do it

 Long rank = (Long) temp[0]; 
+4
source share
1 answer

The createNativeQuery method creates an instance of the query. You must call getResultList () or getSingleResult () in the Query object to actually execute the query.

http://download.oracle.com/javaee/5/api/javax/persistence/Query.html

+1
source

All Articles