Search code examples
javaperformancehibernate

In hibernate statistics whats the difference between load and fetch


Im mainly looking at the EntityStatics (http://www.hibernate.org/hib_docs/v3/api/org/hibernate/stat/EntityStatistics.html). I can see a lot of fetch, loads and updates and i cant find anywhere that says what the difference between them are.


Solution

  • Working backward through the code, the fetch counter only gets incremented when the entity is retrieved from the datasource (as opposed to any caches) -

        protected Object loadFromDatasource(
            final LoadEvent event,
            final EntityPersister persister,
            final EntityKey keyToLoad,
            final LoadEventListener.LoadType options) {
        final SessionImplementor source = event.getSession();
        Object entity = persister.load(
                event.getEntityId(),
                event.getInstanceToLoad(),
                event.getLockMode(),
                source
        );
    
        if ( event.isAssociationFetch() && source.getFactory().getStatistics().isStatisticsEnabled() ) {
            source.getFactory().getStatisticsImplementor().fetchEntity( event.getEntityClassName() );
        }
    
        return entity;
    }
    

    The load counter was called from too many places to track them all down, but it looks like it gets incremented any time the entity gets loaded, whether from the datasource or the caches.