Example usage for org.hibernate.persister.collection CollectionPersister getSize

List of usage examples for org.hibernate.persister.collection CollectionPersister getSize

Introduction

In this page you can find the example usage for org.hibernate.persister.collection CollectionPersister getSize.

Prototype

int getSize(Serializable key, SharedSessionContractImplementor session);

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   ww w. j  a  va2s . c  o 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.eclipse.emf.cdo.server.internal.hibernate.tuplizer.WrappedHibernateList.java

License:Open Source License

public int size() {
    if (cachedSize != -1) {
        return cachedSize;
    }// w w  w  .ja va 2s  .  co 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();
}