This week's book giveaway is in the Features new in Java 9 forum.
We're giving away four copies of Java 9 Revealed and have Kishori Sharan on-line!
See this thread for details.
Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

NamedQuery: Retrieve more than one field?!?  RSS feed

 
Johannes Salter
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why won't it let you cast?
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JPA a query with multiple selected values will return a Object[].

Use,
Object[] result = (Object[]) q.setParameter("name", myUserId ).getSingleResult();
 
Johannes Salter
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it *will* let you cast.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!