File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when ObjectNotFoundException happens? when NoSuchEntityException happens?

 
Hai Lin
Ranch Hand
Posts: 79
  • 0
  • 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
 
Alex Sharkoff
Ranch Hand
Posts: 209
  • 0
  • 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
  • 0
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic