Make delicious recipes!

Difference between Session.get() and Session.load()

Search for setAllowProxyCreation in
Notice that the value is true for load() and not for get().
This value is used via getAllowProxyCreation() in as:
     * Based on configured options, will either return a pre-existing proxy,
     * generate a new proxy, or perform an actual load.
     * @param event The initiating load request event
     * @param persister The persister corresponding to the entity to be loaded
     * @param keyToLoad The key of the entity to be loaded
     * @param options The defined load options
     * @return The result of the proxy/load operation.
    protected Object proxyOrLoad(
            final LoadEvent event,
            final EntityPersister persister,
            final EntityKey keyToLoad,
            final LoadEventListener.LoadType options) {

        if ( LOG.isTraceEnabled() ) {
                    "Loading entity: {0}",
                    MessageHelper.infoString( persister, event.getEntityId(), event.getSession().getFactory() )

        // this class has no proxies (so do a shortcut)
        if ( !persister.hasProxy() ) {
            return load( event, persister, keyToLoad, options );

        final PersistenceContext persistenceContext = event.getSession().getPersistenceContext();

        // look for a proxy
        Object proxy = persistenceContext.getProxy( keyToLoad );
        if ( proxy != null ) {
            return returnNarrowedProxy( event, persister, keyToLoad, options, persistenceContext, proxy );

        if ( options.isAllowProxyCreation() ) {
            return createProxyIfNecessary( event, persister, keyToLoad, options, persistenceContext );

        // return a newly loaded object
        return load( event, persister, keyToLoad, options );
* All code here is from Hibernate version 4.3.1.Final

This means that only for load(), Hibernate allows proxy creation and not for get().
Proxies are nothing but a place-holder for an entity that is supposed to be loaded on-demand from the Database.
This is done to improve performance.

Another important points to notice in the above code:
  1. persister.hasProxy()
    This translates to the following piece of code in
        lazy = persistentClass.isLazy() && (
            // TODO: this disables laziness even in non-pojo entity modes:
            !persistentClass.hasPojoRepresentation() ||
            !ReflectHelper.isFinalClass( persistentClass.getProxyInterface() )
    In plain English, this means that lazy is true if
         @Proxy annotation is setting lazy to true and the proxy-class mentioned by it is non-final.
    Note: !A || !B above is read better as !(A && B)

  2. Object proxy = persistenceContext.getProxy( keyToLoad );
    createProxyIfNecessary creates proxies and puts them in a Map whose key is the entity-ID.
    The above call to getProxy() tries to load the proxy from that map.

How does Hibernate create proxies

Hibernate uses JavassistLazyInitializer for creating proxies using Javaassist


Proxies are used for performance and are created when:
  1. Session.load() is used instead of Session.get()
  2. Annotation @Proxy is used with lazy=true.
However, proxy-creation is not without disadvantages:
  1. Since proxy object does not represent actual object, its hash-code and equals may behave differently.
  2. Hibernate creates proxies to avoid loading from DB and so does not check if a corresponding object exists in the DB.
    So use Session.load() only for those objects where its guaranteed that a backing object is there in the DB else update calls will fail.

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal