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

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

Introduction

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

Prototype

public final SharedSessionContractImplementor getSession() 

Source Link

Document

Get the session currently associated with this collection.

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;
    }/* w w w .j av  a2s  .  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);
}