• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning only one value from JPA query

 
Jrcastro Ribeiro
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!

When a execute that method that error appears: "More than one result was returned from Query.getSingleResult()"

I thought that using the getSingleResult() was the solution, but a was wrong.

Any ideas?


idordemservico = primary key

 
Paul Clapham
Sheriff
Posts: 21588
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the Ranch!

When you say "I thought that using the getSingleResult() was the solution", what is the question which you thought it was the solution to?
 
Jrcastro Ribeiro
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Hi, welcome to the Ranch!

When you say "I thought that using the getSingleResult() was the solution", what is the question which you thought it was the solution to?



i thought that was the solution, I tried using setmaxValue but dint't work, and now, im dont have any more ideais, how i can resolve the problem =/
 
Paul Clapham
Sheriff
Posts: 21588
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jrcastro Ribeiro wrote:how i can resolve the problem


You still haven't said what the problem is!
 
Jrcastro Ribeiro
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Jrcastro Ribeiro wrote:how i can resolve the problem


You still haven't said what the problem is!



The problem is: That query is Returning a list of elements from the data base, and only need the frist element of that list.

When i use: id =Integer.parseInt(em.createQuery(sql).getSingleResult().toString()); that error appears: "More than one result was returned from Query.getSingleResult()"


I think the problem is with the getSingleResult() method.
 
Knute Snortum
Bartender
Pie
Posts: 2912
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getSingleResult() is a way of checking that you have exactly one result from the query. Note the Java doc:

getSingleResult
Object getSingleResult()
Execute a SELECT query that returns a single untyped result.
Returns:
the result
Throws:
NoResultException - if there is no result
NonUniqueResultException - if more than one result


So it is designed to throw an exception if there is more than one result. So change your SQL to something like:

 
Jrcastro Ribeiro
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:getSingleResult() is a way of checking that you have exactly one result from the query. Note the Java doc:

getSingleResult
Object getSingleResult()
Execute a SELECT query that returns a single untyped result.
Returns:
the result
Throws:
NoResultException - if there is no result
NonUniqueResultException - if more than one result


So it is designed to throw an exception if there is more than one result. So change your SQL to something like:




Thank you for the help, but a think JPA don't accepted the "limit" parameter =/
 
Knute Snortum
Bartender
Pie
Posts: 2912
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without looking too hard, I found this:



So setMaxResults(1).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jrcastro Ribeiro wrote:I think the problem is with the getSingleResult() method.


Perhaps there is another method that does what you seek? Have you pursued the javadoc?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic