Example usage for org.hibernate.persister.collection QueryableCollection getElementByIndex

List of usage examples for org.hibernate.persister.collection QueryableCollection getElementByIndex

Introduction

In this page you can find the example usage for org.hibernate.persister.collection QueryableCollection getElementByIndex.

Prototype

Object getElementByIndex(Serializable key, Object index, SharedSessionContractImplementor session,
            Object owner);

Source Link

Usage

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   www.j  a  va  2 s  .  co  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;
}