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

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

Introduction

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

Prototype

public final SharedSessionContractImplementor getSession() 

Source Link

Document

Get the session currently associated with this collection.

Usage

From source file:ch.algotrader.dao.HibernateInitializer.java

License:Open Source License

@Override
public <T extends BaseEntityI, C extends Collection<T>> C initializeCollection(BaseEntityI entity,
        String context, C col) {/*from w ww. j  a v  a2  s  . com*/

    if (col instanceof AbstractPersistentCollection) {

        AbstractPersistentCollection persistentCol = (AbstractPersistentCollection) col;
        if (!persistentCol.wasInitialized()) {
            if (persistentCol.getSession() != null) {

                long before = System.nanoTime();
                persistentCol.forceInitialization();
                MetricsUtil.account(ClassUtils.getShortClassName(entity.getClass()) + context, (before));
            } else {
                throw new IllegalStateException("no hibernate session available");
            }
        }
    }

    return col;
}

From source file:org.babyfish.hibernate.collection.type.AbstractMACollectionType.java

License:Open Source License

protected static void throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection c) {
    if (!isConnectedToSession(c)) {
        throwLazyInitializationException(c, "no session or session was closed");
    }//from  w  w  w  .  j a  v a2 s. c  om
    if (!c.getSession().isConnected()) {
        throwLazyInitializationException(c, "session is disconnected");
    }
}

From source file:org.babyfish.hibernate.collection.type.AbstractMACollectionType.java

License:Open Source License

protected static boolean isConnectedToSession(AbstractPersistentCollection c) {
    SessionImplementor session = c.getSession();
    return session != null && session.isOpen() && session.getPersistenceContext().containsCollection(c);
}

From source file:org.eclipse.emf.cdo.server.internal.hibernate.tuplizer.WrappedHibernateList.java

License:Open Source License

protected boolean isConnectedToSession() {
    final AbstractPersistentCollection persistentCollection = (AbstractPersistentCollection) delegate;
    final SessionImplementor session = persistentCollection.getSession();
    return session != null && session.isOpen()
            && session.getPersistenceContext().containsCollection(persistentCollection);
}

From source file:org.eclipse.emf.cdo.server.internal.hibernate.tuplizer.WrappedHibernateList.java

License:Open Source License

public int size() {
    if (cachedSize != -1) {
        return cachedSize;
    }// w  w w . j a v a  2 s .c  o m
    if (getDelegate() instanceof AbstractPersistentCollection) {
        final AbstractPersistentCollection collection = (AbstractPersistentCollection) getDelegate();
        if (collection.wasInitialized()) {
            cachedSize = -1;
            return getDelegate().size();
        }
        final SessionImplementor session = collection.getSession();
        CollectionEntry entry = session.getPersistenceContext().getCollectionEntry(collection);
        CollectionPersister persister = entry.getLoadedPersister();
        if (collection.hasQueuedOperations()) {
            session.flush();
        }
        cachedSize = persister.getSize(entry.getLoadedKey(), session);
        return cachedSize;
    }

    return getDelegate().size();
}

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 .  jav a  2 s.  c o m
        } 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);
    }
}