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

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

Introduction

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

Prototype

@Override
    public final String getRole() 

Source Link

Usage

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

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override//from w  ww  .  j  av  a 2s  .  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 w w  . j a  v a 2 s  .  com*/

    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:org.babyfish.hibernate.collection.type.AbstractMACollectionType.java

License:Open Source License

protected static void throwLazyInitializationException(AbstractPersistentCollection c, String message) {
    throw new LazyInitializationException("failed to lazily initialize a collection"
            + (c.getRole() == null ? "" : " of role: " + c.getRole()) + ", " + message);
}