In case of transaction scoped persistence context:
An entity will be loaded into the persistence context, if it is fetched in the scope of a transaction.
And it will get detached , the context gets closed, when the transaction ends.
So, I believe that, if the code block that you have posted is running in the scope of a tx, the contains() call that is made should always return true for any entity.
( SCBCD 5, CCENT, SCJP 5 )
Joined: Jun 29, 2011
Sorry for the late answer. Thanks for your reply.
Your explanation is right if the entity exists. But I am trying to handle the case when it doesn't exist. It works fine in the true case (=when .contains returns true) but doesn't catch the null pointer exception.