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?
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)
posted 16 years ago
Alex, Thanks a lot. Your explanation is very clear and elaborated. Bettern than the HFEJB Greatly appreciate your reply.
permaculture is largely about replacing oil with people. And one tiny ad:
Devious Experiments for a Truly Passive Greenhouse!