Example usage for org.hibernate.engine.spi CollectionEntry getLoadedKey

List of usage examples for org.hibernate.engine.spi CollectionEntry getLoadedKey

Introduction

In this page you can find the example usage for org.hibernate.engine.spi CollectionEntry getLoadedKey.

Prototype

public Serializable getLoadedKey() 

Source Link

Usage

From source file:com.amalto.core.storage.hibernate.TypeMapping.java

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
protected static <T> List<T> getFullList(List valueList) {
    if (valueList == null) {
        return null;
    }/*from   w ww  .j a  v  a2  s  .co m*/
    if (!(valueList instanceof PersistentList)) {
        return valueList;
    }
    PersistentList list = (PersistentList) valueList;
    List<T> fullList = new LinkedList<T>();
    SessionImplementor session = list.getSession();
    if (!session.isConnected()) {
        throw new IllegalStateException("Session is not connected: impossible to read values from database."); //$NON-NLS-1$
    }
    CollectionEntry entry = session.getPersistenceContext().getCollectionEntry(list);
    CollectionPersister persister = entry.getLoadedPersister();
    if (persister != null) {
        int databaseSize = persister.getSize(entry.getKey(), session);
        if (list.size() == databaseSize && !list.contains(null)) {
            // No need to reload a list (no omission in list and size() corresponds to size read from database).
            return list;
        }
        for (int i = 0; i < databaseSize; i++) {
            T wrapper = (T) persister.getElementByIndex(entry.getLoadedKey(), i, session, list.getOwner());
            fullList.add(wrapper);
        }
    }
    // Returns a unmodifiable list -> returned list is *not* a persistent list so change tracking is not possible,
    // returning a unmodifiable list is a safety for code using returned list.
    return Collections.unmodifiableList(fullList);
}

From source file:org.babyfish.hibernate.collection.spi.persistence.AbstractBasePersistence.java

License:Open Source License

@Override
public final boolean setCurrentSession(SessionImplementor session) throws HibernateException {
    if (session == this.getSession()) {
        return false;
    } else {//w  w  w  .  j  av a  2s.co  m
        if (this.isConnectedToSession()) {
            CollectionEntry ce = session.getPersistenceContext()
                    .getCollectionEntry(this.getWrapperPersistentCollection());
            if (ce == null) {
                throw new HibernateException(
                        "Illegal attempt to associate a collection with two open sessions");
            } else {
                throw new HibernateException(
                        "Illegal attempt to associate a collection with two open sessions: "
                                + MessageHelper.collectionInfoString(ce.getLoadedPersister(), ce.getLoadedKey(),
                                        session.getFactory()));
            }
        } else {
            this.setSession(session);
            return true;
        }
    }
}

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

License:Open Source License

public List<Chunk> executeRead() {
    // get a transaction, the hibernateStoreAccessor is placed in a threadlocal
    // so all db access uses the same session.
    final Session session = getAccessor().getHibernateSession();

    // reread the revision as it is probably unreferenced
    final InternalCDORevision latestRevision = getLatestRevision(session);
    Object value = latestRevision.getValue(getFeature());
    if (value instanceof WrappedHibernateList) {
        value = ((WrappedHibernateList) value).getDelegate();
    }//from w  w  w .  j a  va2s . c  o  m

    // hibernate details...
    boolean useExtraLazyMode = false;
    boolean standardCDOList = false;
    QueryableCollection persister = null;
    CollectionEntry entry = null;
    if (value instanceof PersistentCollection) {
        final PersistentCollection persistentCollection = (PersistentCollection) value;
        persister = (QueryableCollection) ((SessionFactoryImplementor) session.getSessionFactory())
                .getCollectionPersister(persistentCollection.getRole());
        entry = ((SessionImplementor) session).getPersistenceContext().getCollectionEntry(persistentCollection);

        useExtraLazyMode = !persister.getElementType().isEntityType();
        if (useExtraLazyMode && ((PersistentCollection) value).hasQueuedOperations()) {
            session.flush();
        }
    } else {
        standardCDOList = true;
    }

    final List<Chunk> chunks = getChunks();
    for (Chunk chunk : chunks) {
        final int startIndex = chunk.getStartIndex();
        final int maxElements = chunk.size();
        if (standardCDOList) {
            // for eattributes just read them all, no chunking there...
            final CDOList list = (CDOList) value;
            if (startIndex >= list.size()) {
                return chunks;
            }
            for (int i = startIndex; i < startIndex + maxElements; i++) {
                if (i >= list.size()) {
                    break;
                }
                addToChunk(chunk, i - startIndex, list.get(i));
            }
        } else if (useExtraLazyMode) {
            if (getFeature() instanceof EReference) {
                for (int i = startIndex; i < startIndex + maxElements; i++) {
                    final Object object = persister.getElementByIndex(entry.getLoadedKey(), i,
                            (SessionImplementor) session, latestRevision);
                    // could happen if the index > size)
                    if (object == null) {
                        continue;
                    }
                    addToChunk(chunk, i - startIndex, object);
                }
            } else {
                // for eattributes just read them all, no chunking there...
                final List<?> list = (List<?>) value;
                if (startIndex >= list.size()) {
                    return chunks;
                }
                for (int i = startIndex; i < startIndex + maxElements; i++) {
                    if (i >= list.size()) {
                        break;
                    }
                    addToChunk(chunk, i - startIndex, list.get(i));
                }
            }
        } else {
            final Query filterQuery = session.createFilter(value, "");
            filterQuery.setMaxResults(maxElements);
            filterQuery.setFirstResult(startIndex);
            int i = 0;
            for (Object object : filterQuery.list()) {
                addToChunk(chunk, i++, object);
            }
        }
    }
    return chunks;
}

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;
    }//ww  w  .ja va2s  .c om
    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();
}