Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 256
Sam Avatar asked Mar 04 '09 05:03

Sam


People also ask

What is Session load in hibernate?

In session. load(), Hibernate will not hit the database (no select statement in output) to retrieve the Stock object, it will return a Stock proxy object – a fake object with given identify value. In this scenario, a proxy object is enough for to save a stock transaction record.

Which is faster get or load?

The load() method is quicker than the get() method.

What is the difference between Get () and Load () in hibernate?

From the output it's clear that get() returns the object by fetching it from database or from hibernate cache whereas load() just returns the reference of an object that might not actually exists, it loads the data from database or cache only when you access other properties of the object.

What is the difference between Session and SessionFactory in hibernate?

SessionFactory is to create and manage Sessions . Session is to provide a CRUD interface for mapped classes, and also access to the more versatile Criteria API .


1 Answers

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.

like image 85
Alex Zuroff Avatar answered Oct 24 '22 11:10

Alex Zuroff