Hello,

I am trying to figure out how hibernate second level caching works with query cache turned off. I debugged through the source and have a primitive idea of the flow which I have listed below. I am not able to figure out at which point in the code flow objects are being fetched from the second level cache.

Below I have listed the flow for a HQL query execution with Object caching turned on and query caching turned off:

HQL QueryLoader list method is called which calls the Loader List method.

The Loader List method checks as to whether the Query is cacheable or not (In my case this is false as I have disabled query caching) and calls listIgnoreQuerCache.

listIgnoreQueryCache calls getResultList passing result of doList and result trasformers as arguments.

Inside doList doQueryAndInitializeNonLazyColl
ections is called.

doQueryAndInitializeNonLazyCollections calls doQuery.

Inside doQuery the resultSet is got  and the result set is dissected to get the values from the result set. For this getRowFromResultSet is called.

The first line of getRowFromResultSet retrieves the EntityPersister object for the applicable entity.

Here the EntityKey is retrieved and the Object corresponding to the row is got. To do this getRow method is called.

getRow checks as to whether the Entity is already in the Session cache or not. If not gets the Entity using instanceNotYetLoaded.
Inside instanceNotYetLoaded the Class to which the Entity should be mapped to is got.
 
At this point should the code not check as to whether the object is already in the cache or not and if present get the object from the cache as the cache stores objects in their dehydrated form and this is what we are doing here.

At this point a new Object corresponding to the Entity is instantiated with the Object having only the Primary Key with no other values.
Now the object is hydrated.(My guess is we are trying to get the other filed values)

Now loadFromResultSet is called.
Here a Loadable object corresponding to the entity is got.
TwoPhaseLoad.addUninitializedEntity method is called. Here a new Entity Object is created and added to an Internal event cache.

After this the row names are got and the values corresponding to the rows are extracted. This is termed hydrating. Anyways once this is done we have
an array of column values.After this all associations are resolved and then postHydrate is called. Here once again the Entity is added to an event source internal cache(Am not sure
as to whether we are adding it as a new entry or manipulating the old entry. My guess is we are manipulating the old entry).

At this point the result set is done processing. I guess at this point one stage of processing is done (We have an Entity Object and the field values in
hydrated array state) and now we move over to the next stage.

In this stage we get the Entity Object from the cache populated in the previous stage and try to fill the Object with values got from the hydrated array.(Should the cache be
checked at this point?)

As the last part of the second stage I see code to populate the secondary cache with the created objects.

My biggest problem is I am not able to see any code being called in the whole cycle that fetches the Objects from the cache. I see lots of second level cache related code in
DefaultLoadEventListener class but I do not see any of this being called.

Can someone please guide and correct me wherever my understanding is incorrect?
--
Cheers,
Abhi