This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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.
 
 
subject: NamedQuery: Retrieve more than one field?!?
 
Similar Threads
OneToOne Relation Exception
named queries can be defined only on an entity - problem
EJBQL Question
Can a class be mapped to a table and be Embeddable at the same time?
HQL Inner Join problem