aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EJB3 in Action errata - EntityManager.find() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EJB3 in Action errata - EntityManager.find() method" Watch "EJB3 in Action errata - EntityManager.find() method" New topic
Author

EJB3 in Action errata - EntityManager.find() method

Tyronne Fernando
Ranch Hand

Joined: May 11, 2008
Posts: 50
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.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
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.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

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


SCJP, SCJD, SCWCD, OCPJBCD
Krzysztof Koziol
Ranch Hand

Joined: Nov 19, 2006
Posts: 133

From documentation of EntityManager:

Returns:
the found entity instance or null if the entity does not exist


SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCEA/OCMJEA 5.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB3 in Action errata - EntityManager.find() method