Win a copy of AWS Security this week in the Cloud/Virtualization 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
  • 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

when ObjectNotFoundException happens? when NoSuchEntityException happens?

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
Could anyone tell me the difference between ObjectNotFoundException and NoSuchEntityException?
So far, what I know is that ObjectNotFoundException is checked Exception, belonging to application exception, while NoSuchEntityException is unchecked Exception, belonging to system exception.
But both of them are talking about 'the entity you're trying to access is no longer in the database', so how do I know which exception will happen if there's no this entity in the database?

Thanks a lot in advance.

Hai
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai,

javax.ejb.ObjectNotFoundException is a subclass of javax.ejb.FinderException that is thrown from the single finder/select method (the ones that return comp interface rather than a collection) when an object does not exist. As you pointed out it is a standard application exception.
Eg, client calls findByPrimaryKey( Object pk ) but there is no record in the underlying db with the given pk - Container throws ObjectNotFoundException. It means eb instance cannot represent an entity (record in db) that has given pk.

[javax.ejb.NoSuchEntityException] is thrown by the bean code when the entity (record in db) has been removed from the underlying persistence store (db) but the entity bean is still associated with it.

Eg, client calls findByPrimaryKey( Object pk ) and gets back EJBObject (it means that there is a record associated with the given pk). Client calls a business method on this EJBObject - on the container side bean instance is pulled out from the pool, loaded with the entity (record in db) data and invoked a business method that returns a method's result to the client. All OK till now.

Client code does something else for a while and then decides to call another business method on the same EJBObject (same entity bean). However, between first business method invocation and the second one the actual entity (record in db) has been removed from the persistence store by external party (eg, dba logged into the database and deleted a record). Our entity bean is still associated with this entity at the time of the second business method invocation. Container tries to refresh (ejbLoad()) the entity bean with the entity data -> no entity in the persistence store -> NoSuchEntityException.


Hope it helps
 
Hai Lin
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex,
Thanks a lot. Your explanation is very clear and elaborated. Bettern than the HFEJB
Greatly appreciate your reply.

Hai
 
permaculture is largely about replacing oil with people. And one tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic