This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic