This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes javax. persistence. NoResultException Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Reply Bookmark "javax. persistence. NoResultException" Watch "javax. persistence. NoResultException" New topic
Author

javax. persistence. NoResultException

Sam Venkata
Ranch Hand

Joined: Mar 06, 2006
Posts: 68
Hi,
I am new to JPA and I have this issue...
I need to get an entity from the database based on the phone number and if the phone number exists my code works fine but throws javax.persistence.NoResultException: No entity found for query if the number does not. All I want ot do is to return null if the phone number does not exist in the database. Can any body help me with this..

my code is as follows..



Can some body help me with this...

Thanks,
Samanth.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 8142
    
  52

Originally posted by Samanth Marisetty:
try{
if(devicesQuery.getSingleResult()!= null){

result = (User) devicesQuery.getSingleResult();
}
}catch(javax.persistence.NoResultException nre){

}


As per the Javadocs of this API:

Execute a SELECT query that returns a single result.

Returns:
the result
Throws:
NoResultException - if there is no result
NonUniqueResultException - if more than one result
IllegalStateException - if called for a Java Persistence query language UPDATE or DELETE statement



So why dont you try the other getResultList() API by setting the maxResults to 1 as follows:



[My Blog] [JavaRanch Journal]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: javax. persistence. NoResultException
 
Similar Threads
cannot find symbol
security migration from weblogic to Jboss
Problem javax.persistence.NoResultException: No entity found for query
User Authentication Mechanism
Servlet calling EJB - compiling problems