• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB3 in Action errata - EntityManager.find() method

 
Tyronne Fernando
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.
 
Remko Strating
Ranch Hand
Posts: 893
  • 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.

 
Mihai Radulescu
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
 
Krzysztof Koziol
Ranch Hand
Posts: 133
Eclipse IDE Java Spring
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic