• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

EJB3 in Action errata - EntityManager.find() method

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is from the "EJB 3 in Action" page 320.

"Note that if an entity instance matching the specified key does not exist in the database, the find method will not throw any exceptions. Instead, the Entity- Manager will return null or an empty entity and your application must handle this situation."

Is that true that the find() method can return an empty object, if it can not find the coresponding record in the database? Is it up to the persistence provider to decide whether to return a null or an empty object?

Thanks in advance.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My experience is that null will be returned and depending on your logic in your code you can decide to throw an exception or to ignore the null-value. If you ignore this behaviour there is the risk of a null-pointer exception.

 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

in the Java Persistence API, Version 2.0 on page 63 (section 3.3.1) you can find :


...
* @return the found entity instance or null if the entity does not exist.
...



I don't think that the Em will return an empty object.


Regards,
Mihai
 
Ranch Hand
Posts: 133
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From documentation of EntityManager:

Returns:
the found entity instance or null if the entity does not exist
 
I am Arthur, King of the Britons. And this is a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic