Example usage for org.hibernate.collection.internal AbstractPersistentCollection setCurrentSession

List of usage examples for org.hibernate.collection.internal AbstractPersistentCollection setCurrentSession

Introduction

In this page you can find the example usage for org.hibernate.collection.internal AbstractPersistentCollection setCurrentSession.

Prototype

@Override
    public final boolean setCurrentSession(SharedSessionContractImplementor session) throws HibernateException 

Source Link

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);
            }//  w  w  w.j a  va2s . 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);
    }
}