File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets 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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NamedQuery: Retrieve more than one field?!?
 
Similar Threads
EJBQL Question
Can a class be mapped to a table and be Embeddable at the same time?
HQL Inner Join problem
OneToOne Relation Exception
named queries can be defined only on an entity - problem