Example usage for org.hibernate.proxy LazyInitializer initialize

List of usage examples for org.hibernate.proxy LazyInitializer initialize

Introduction

In this page you can find the example usage for org.hibernate.proxy LazyInitializer initialize.

Prototype

void initialize() throws HibernateException;

Source Link

Document

Initialize the proxy, fetching the target entity if necessary.

Usage

From source file:de.innovationgate.webgate.api.jdbc.WGDocumentImpl.java

License:Open Source License

public void makeEditable() throws WGAPIException {

    if (_editable == false) {
        if (_parent.getSessionStatus().isInSaveOperation()) { // Do not make editable if already in save operation
            return;
        }//from   w w  w  .j  a  va 2 s  .co m

        if (_parent._saveIsolationActive) {
            _entity.loadFully();
            if (_entity instanceof HibernateProxy) { // Unproxying because of #00002040
                HibernateProxy proxy = (HibernateProxy) _entity;
                LazyInitializer lazyInitializer = proxy.getHibernateLazyInitializer();
                if (lazyInitializer.isUninitialized()) {
                    lazyInitializer.initialize();
                }
                _entity = (MainEntity) lazyInitializer.getImplementation();
                _parent.getSession().evict(proxy);
            } else {

                _parent.getSession().evict(_entity);
            }

        }
        _editable = true;
    }

}

From source file:nl.strohalm.cyclos.utils.hibernate.HibernateQueryHandler.java

License:Open Source License

/**
 * Initialize an entity or collection//  w ww  . j  a  v a  2  s .  c om
 */
public Object initialize(final Object object) {
    if (object instanceof HibernateProxy) {
        // Reassociate the entity with the current session
        Entity entity = (Entity) object;
        entity = getHibernateTemplate().load(EntityHelper.getRealClass(entity), entity.getId());
        // Return the implementation associated with the proxy
        if (entity instanceof HibernateProxy) {
            final LazyInitializer lazyInitializer = ((HibernateProxy) entity).getHibernateLazyInitializer();
            lazyInitializer.initialize();
            return lazyInitializer.getImplementation();
        } else {
            return entity;
        }
    } else if (object instanceof PersistentCollection) {
        // Reassociate the collection with the current session
        return getHibernateTemplate().execute(new HibernateCallback<Object>() {
            //@Override
            public Object doInHibernate(final Session session) throws HibernateException, SQLException {
                final PersistentCollection persistentCollection = ((PersistentCollection) object);
                Entity owner = (Entity) persistentCollection.getOwner();
                final String role = persistentCollection.getRole();
                if (owner == null || role == null) {
                    return persistentCollection;
                }
                // Retrieve the owner of this persistent collection, associated with the current session
                owner = (Entity) session.get(EntityHelper.getRealClass(owner), owner.getId());
                // Retrieve the collection through it's role (property name)
                final String propertyName = PropertyHelper.lastProperty(role);
                final Object currentCollection = PropertyHelper.get(owner, propertyName);
                if (currentCollection instanceof PersistentCollection) {
                    Hibernate.initialize(currentCollection);
                }
                return currentCollection;
            }
        });
    }
    try {
        Hibernate.initialize(object);
    } catch (final ObjectNotFoundException e) {
        throw new EntityNotFoundException();
    }
    return object;
}

From source file:org.babyfish.hibernate.proxy.FrozenLazyInitializerImpl.java

License:Open Source License

@Override
public void initialize() throws HibernateException {
    LazyInitializer rawLazyInitializer = this.lazyInitializer;
    Object oldTarget = rawLazyInitializer.isUninitialized() ? null : rawLazyInitializer.getImplementation();
    rawLazyInitializer.initialize();
    Object target = rawLazyInitializer.getImplementation();
    if (oldTarget != target) {
        ScalarListener listener = this.new TargetScalarListener();
        if (oldTarget != null) {
            this.omFactory.get(oldTarget).removeScalarListener(listener);
        }/* ww w  . ja  v  a 2  s .  co  m*/
        this.oldTarget = target;
        if (target != null) {
            ObjectModel targetOM = this.omFactory.get(target);
            targetOM.removeScalarListener(listener); //remove the duplicated listener.
            targetOM.addScalarListener(listener);
        }
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.proxy.HibernateProxyHandler.java

License:Apache License

public Object unwrapProxy(final HibernateProxy proxy) {
    final LazyInitializer lazyInitializer = proxy.getHibernateLazyInitializer();
    if (lazyInitializer.isUninitialized()) {
        lazyInitializer.initialize();
    }/*  w w w  .  j av a2  s  . c o m*/
    final Object obj = lazyInitializer.getImplementation();
    if (obj != null) {
        GrailsHibernateUtil.ensureCorrectGroovyMetaClass(obj, obj.getClass());
    }
    return obj;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.proxy.HibernateProxyHandler.java

License:Apache License

public void initialize(Object o) {
    if (o instanceof HibernateProxy) {
        final LazyInitializer hibernateLazyInitializer = ((HibernateProxy) o).getHibernateLazyInitializer();
        if (hibernateLazyInitializer.isUninitialized()) {
            hibernateLazyInitializer.initialize();
        }/* www.j a v  a  2s  .c  o m*/
    } else if (o instanceof AbstractPersistentCollection) {
        final AbstractPersistentCollection col = (AbstractPersistentCollection) o;
        if (!col.wasInitialized()) {
            col.forceInitialization();
        }
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.proxy.SimpleHibernateProxyHandler.java

License:Apache License

public Object unwrapProxy(final HibernateProxy proxy) {
    final LazyInitializer lazyInitializer = proxy.getHibernateLazyInitializer();
    if (lazyInitializer.isUninitialized()) {
        lazyInitializer.initialize();
    }/*  w w  w.j  a  va2s .c o  m*/
    final Object obj = lazyInitializer.getImplementation();
    if (obj != null) {
        ensureCorrectGroovyMetaClass(obj, obj.getClass());
    }
    return obj;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.proxy.SimpleHibernateProxyHandler.java

License:Apache License

public void initialize(Object o) {
    if (o instanceof HibernateProxy) {
        final LazyInitializer hibernateLazyInitializer = ((HibernateProxy) o).getHibernateLazyInitializer();
        if (hibernateLazyInitializer.isUninitialized()) {
            hibernateLazyInitializer.initialize();
        }//from  ww  w . ja va 2s.  c om
    }
}

From source file:org.web4thejob.orm.MetaReaderServiceImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <E extends Entity> E deproxyEntity(E entity) {
    final E proxy = entity;

    if (proxy instanceof HibernateProxy) {
        {/*from  w  w w.j av  a2 s  . c o m*/
            if (((HibernateProxy) proxy).getHibernateLazyInitializer().isUninitialized()) {
                final E impl = ContextUtil.getTransactionWrapper().execute(new TransactionCallback<E>() {
                    @Override
                    public E doInTransaction(TransactionStatus status) {
                        final LazyInitializer lazy = ((HibernateProxy) proxy).getHibernateLazyInitializer();

                        lazy.setSession((SessionImplementor) sessionFactory.getCurrentSession());
                        lazy.initialize();
                        final Object impl = lazy.getImplementation();
                        lazy.unsetSession();
                        return (E) impl;
                    }
                });
                return impl;
            } else {
                return (E) ((HibernateProxy) proxy).getHibernateLazyInitializer().getImplementation();
            }
        }
    }

    return entity;
}