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

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

Introduction

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

Prototype

@Override
    public final boolean wasInitialized() 

Source Link

Usage

From source file:ch.algotrader.cache.CollectionHandler.java

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override/*from   w  w w .j a v  a  2  s  . c om*/
protected CacheResponse put(Object obj, List<EntityCacheSubKey> stack) {

    if (obj instanceof AbstractPersistentCollection) {
        AbstractPersistentCollection col = (AbstractPersistentCollection) obj;

        // do not process uninitialized PersistentCollections
        if (!col.wasInitialized()) {
            return CacheResponse.skippedObject();
        }

        // check stack on Persistent Collections with role only
        if (col.getRole() != null) {
            EntityCacheSubKey cacheKey = new EntityCacheSubKey((BaseEntityI) col.getOwner(), col.getRole());
            if (stack.contains(cacheKey)) {
                return CacheResponse.processedObject();
            } else {
                stack.add(cacheKey);
            }
        }
    }

    synchronized (obj) {

        // process Maps
        if (obj instanceof Map) {

            Map map = (Map) obj;
            for (Object o : map.entrySet()) {

                Map.Entry entry = (Map.Entry) o;
                Object value = entry.getValue();
                CacheResponse response = this.cacheManager.put(value, stack);
                if (response.getState() == CacheState.EXISTING && response.getValue() != value) {

                    // replace the value with the existingValue
                    entry.setValue(response.getValue());
                }
            }

            // process Lists
        } else if (obj instanceof List) {

            List list = (List) obj;
            for (ListIterator it = list.listIterator(); it.hasNext();) {

                Object value = it.next();
                CacheResponse response = this.cacheManager.put(value, stack);
                if (response.getState() == CacheState.EXISTING && response.getValue() != value) {

                    // replace the value with the existingValue
                    it.set(response.getValue());
                }
            }

            // process Sets
        } else if (obj instanceof Set) {

            Set set = (Set) obj;
            for (Object value : set) {

                this.cacheManager.put(value, stack);

                // values are not replaced in the set since objects might not be initialized enough for equals/hashCode need by Set.add()
            }

        } else {
            throw new IllegalArgumentException("unsupported collection type " + obj.getClass());
        }
    }
    return CacheResponse.newObject();
}

From source file:ch.algotrader.cache.CollectionHandler.java

License:Open Source License

@Override
protected CacheResponse initialize(Object obj) {

    if (!(obj instanceof AbstractPersistentCollection)) {
        throw new IllegalArgumentException("none PersistentCollection passed " + obj);
    }//from w ww. j  a  v a 2 s  .co m

    AbstractPersistentCollection col = (AbstractPersistentCollection) obj;
    if (col.wasInitialized()) {
        throw new IllegalArgumentException("PersistentCollection is already initialized " + obj);
    }

    if (col.getRole() == null) {
        throw new IllegalArgumentException("missing role on " + obj);
    }

    synchronized (obj) {

        Object initializedObj = this.cacheManager.getGenericDao().getInitializedCollection(col.getRole(),
                (Long) col.getKey());

        CacheResponse response = this.cacheManager.put(initializedObj);

        if (response.getState() == CacheState.EXISTING) {
            return response;
        } else {
            return CacheResponse.updatedObject(initializedObj);
        }
    }
}

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) {// w  w  w .  j av a 2 s. co m

    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.eclipse.emf.cdo.server.internal.hibernate.tuplizer.WrappedHibernateList.java

License:Open Source License

public int size() {
    if (cachedSize != -1) {
        return cachedSize;
    }//  www . j a  va 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();
}