File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes when ObjectNotFoundException happens? when NoSuchEntityException happens? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "when ObjectNotFoundException happens? when NoSuchEntityException happens?" Watch "when ObjectNotFoundException happens? when NoSuchEntityException happens?" New topic
Author

when ObjectNotFoundException happens? when NoSuchEntityException happens?

Hai Lin
Ranch Hand

Joined: May 23, 2004
Posts: 79
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

Joined: Apr 11, 2004
Posts: 209
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


Alex (SCJP 1.4, SCBCD 1.3, SCWCD 1.4, SCJD 1.4)
Hai Lin
Ranch Hand

Joined: May 23, 2004
Posts: 79
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
 
subject: when ObjectNotFoundException happens? when NoSuchEntityException happens?