aspose file tools*
The moose likes Object Relational Mapping and the fly likes NamedQuery: Retrieve more than one field?!? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "NamedQuery: Retrieve more than one field?!?" Watch "NamedQuery: Retrieve more than one field?!?" New topic
Author

NamedQuery: Retrieve more than one field?!?

Johannes Salter
Greenhorn

Joined: May 02, 2010
Posts: 15
Howdy!

I'm learning Java and database handling, and I'm having problems working with named querys.

Query A:
Query B:
(...)
@NamedQuery(name = "AuthTable.getUserNames", query = "SELECT a.firstName, a.birthName, a.lastName FROM AuthTable a WHERE a.name = :name")})
(...)


I retrieve the information with:When I use the first NamedQuery I can cast "Object result" to "String" and everything goes well.
With the second query JAVA won't let me cast "Object result" to anything. Not to "Array", not to "List", not to anything. "Object result" is useless for me!

When I use "q.getResultList()" I get a list with one element, which equals to "Object result", that is useless for me here too.

Any help appreciated.
Thanks!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why won't it let you cast?
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
In JPA a query with multiple selected values will return a Object[].

Use,
Object[] result = (Object[]) q.setParameter("name", myUserId ).getSingleResult();


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Johannes Salter
Greenhorn

Joined: May 02, 2010
Posts: 15
James Sutherland wrote:In JPA a query with multiple selected values will return a Object[].

Use,
Object[] result = (Object[]) q.setParameter("name", myUserId ).getSingleResult();


You are my hero.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

So it *will* let you cast.
 
wood burning stoves
 
subject: NamedQuery: Retrieve more than one field?!?