Example usage for org.hibernate.proxy LazyInitializer setSession

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

Introduction

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

Prototype

void setSession(SharedSessionContractImplementor session) throws HibernateException;

Source Link

Document

Associate the proxy with the given session.

Usage

From source file:org.jspresso.framework.application.backend.persistence.hibernate.HibernateBackendController.java

License:Open Source License

@SuppressWarnings("unchecked")
private void performPropertyInitializationUsingSession(final IComponent componentOrEntity,
        final String propertyName, Session hibernateSession) {
    Object propertyValue = componentOrEntity.straightGetProperty(propertyName);
    if (!isInitialized(propertyValue)) {
        IEntityRegistry alreadyDetached = createEntityRegistry("detachFromHibernateInDepth");
        if (componentOrEntity instanceof IEntity) {
            if (((IEntity) componentOrEntity).isPersistent()) {
                detachFromHibernateInDepth(componentOrEntity, hibernateSession, alreadyDetached);
                lockInHibernate((IEntity) componentOrEntity, hibernateSession);
            } else if (IEntity.DELETED_VERSION.equals(((IEntity) componentOrEntity).getVersion())) {
                LOG.error("Trying to initialize a property ({}) on a deleted object ({} : {}).", propertyName,
                        componentOrEntity.getComponentContract().getName(), componentOrEntity);
                throw new RuntimeException("Trying to initialize a property (" + propertyName
                        + ") on a deleted object (" + componentOrEntity.getComponentContract().getName() + " : "
                        + componentOrEntity + ")");
            } else if (propertyValue instanceof IEntity) {
                detachFromHibernateInDepth((IEntity) propertyValue, hibernateSession, alreadyDetached);
                lockInHibernate((IEntity) propertyValue, hibernateSession);
            }/*from w w  w  .ja v a 2 s.com*/
        } else if (propertyValue instanceof IEntity) {
            // to handle initialization of component properties.
            detachFromHibernateInDepth((IEntity) propertyValue, hibernateSession, alreadyDetached);
            lockInHibernate((IEntity) propertyValue, hibernateSession);
        }

        if (propertyValue instanceof HibernateProxy) {
            HibernateProxy proxy = (HibernateProxy) propertyValue;
            LazyInitializer li = proxy.getHibernateLazyInitializer();
            if (li.getSession() == null) {
                try {
                    li.setSession((SessionImplementor) hibernateSession);
                } catch (Exception ex) {
                    LOG.error(
                            "An internal error occurred when re-associating Hibernate session for {} reference initialization.",
                            propertyName);
                    LOG.error("Source exception", ex);
                }
            }
        } else if (propertyValue instanceof AbstractPersistentCollection) {
            AbstractPersistentCollection apc = (AbstractPersistentCollection) propertyValue;
            if (apc.getSession() == null) {
                try {
                    apc.setCurrentSession((SessionImplementor) hibernateSession);
                } catch (Exception ex) {
                    LOG.error(
                            "An internal error occurred when re-associating Hibernate session for {} reference initialization.",
                            propertyName);
                    LOG.error("Source exception", ex);
                }
            }
        }
        Hibernate.initialize(propertyValue);
        if (propertyValue instanceof Collection<?> && propertyValue instanceof PersistentCollection) {
            relinkAfterInitialization((Collection<?>) propertyValue, componentOrEntity);
            for (Iterator<?> ite = ((Collection<?>) propertyValue).iterator(); ite.hasNext();) {
                Object collectionElement = ite.next();
                if (collectionElement instanceof IEntity) {
                    if (isEntityRegisteredForDeletion((IEntity) collectionElement)) {
                        ite.remove();
                    }
                }
            }
        } else {
            relinkAfterInitialization(Collections.singleton(propertyValue), componentOrEntity);
        }
        clearPropertyDirtyState(propertyValue);
    }
}

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  a  v  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;
}