• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

javax. persistence. NoResultException

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

 
reply
    Bookmark Topic Watch Topic
  • New Topic