Hi all,
in our application, we are doing a manual prefetching of entity objects which we know we will be using. We do so based on the entity's IDs using a simple HQL query like the following:
Code:
from Entity e where e.id in (:ids)
The drawback is that some of the entities loaded by the query may already be in the cache so loading them again makes no sense. Therefore, we want to exclude the entities which are already in the cache from the query.
My question is how I can check whether an entity is already loaded in the 1st or 2nd level cache if I only have the entity's ID? I already know of the Hibernate#isInitialized method but there I need to supply the entity object itself as parameter and not its ID. Also, first getting the entity object through Session#get(Class entityClazz, Serializable id) and then calling Hibernate#isInitialized does not work if the entity class does not allow proxies to be created by Hibernate since then, calling Session#get would already trigger a database query in case the entity object was not loaded yet.
Any idea how to achieve this? Thanks in advance for any help!