Example usage for org.hibernate.collection.spi PersistentCollection getKey

List of usage examples for org.hibernate.collection.spi PersistentCollection getKey

Introduction

In this page you can find the example usage for org.hibernate.collection.spi PersistentCollection getKey.

Prototype

Serializable getKey();

Source Link

Document

Get the current collection key value

Usage

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

License:Open Source License

@Override
protected CacheResponse update(Object obj) {

    if (!(obj instanceof PersistentCollection)) {
        throw new IllegalArgumentException("none PersistentCollection passed " + obj);
    }//  www  .  j av a 2  s. com

    PersistentCollection origCollection = (PersistentCollection) obj;

    // sometimes there is no role so collection initialization will not work
    if (origCollection.getRole() == null) {
        return CacheResponse.skippedObject();
    }

    synchronized (obj) {

        Object updatedCollection = this.cacheManager.getGenericDao()
                .getInitializedCollection(origCollection.getRole(), (Long) origCollection.getKey());

        // owner does not exist anymore so remove it
        if (updatedCollection == null) {

            Object owner = origCollection.getOwner();
            EntityCacheKey cacheKey = new EntityCacheKey((BaseEntityI) owner);
            this.cacheManager.getEntityCache().detach(cacheKey);

            return CacheResponse.removedObject();

        } else {

            if (updatedCollection instanceof PersistentCollection) {

                // copy the updatedCollection unto the origCollection in order to update entities holding a reference to the origCollection
                FieldUtil.copyAllFields(updatedCollection, origCollection);

                // make sure all elements of the updateCollection are in the cache
                ArrayList<EntityCacheSubKey> stack = new ArrayList<EntityCacheSubKey>();
                put(updatedCollection, stack);

                // getInitializedCollection should normally return a PersistentCollection
            } else {

                if (updatedCollection instanceof Collection) {
                    Collection<?> col = (Collection<?>) updatedCollection;
                    if (col.size() != 0) {
                        LOGGER.error("non empty collection returned instead of PersistentCollection");
                    }
                } else if (updatedCollection instanceof Map) {
                    Map<?, ?> map = (Map<?, ?>) updatedCollection;
                    if (map.size() != 0) {
                        LOGGER.error("non empty map returned instead of PersistentCollection");
                    }
                }
            }

            return CacheResponse.updatedObject(updatedCollection);
        }
    }
}

From source file:org.granite.hibernate4.HibernateExternalizer.java

License:Open Source License

protected AbstractExternalizablePersistentCollection newExternalizableCollection(PersistentCollection value) {
    final boolean initialized = Hibernate.isInitialized(value);
    final boolean dirty = value.isDirty();

    AbstractExternalizablePersistentCollection coll = null;

    if (value instanceof PersistentSet)
        coll = new ExternalizablePersistentSet(initialized ? (Set<?>) value : null, initialized, dirty);
    else if (value instanceof PersistentList)
        coll = new ExternalizablePersistentList(initialized ? (List<?>) value : null, initialized, dirty);
    else if (value instanceof PersistentBag)
        coll = new ExternalizablePersistentBag(initialized ? (List<?>) value : null, initialized, dirty);
    else if (value instanceof PersistentMap)
        coll = new ExternalizablePersistentMap(initialized ? (Map<?, ?>) value : null, initialized, dirty);
    else/* w  w w  .  ja  v  a  2 s  . c o  m*/
        throw new UnsupportedOperationException("Unsupported Hibernate collection type: " + value);

    if (serializeMetadata != SerializeMetadata.NO
            && (serializeMetadata == SerializeMetadata.YES || !initialized) && value.getRole() != null) {
        char[] hexKey = StringUtil.bytesToHexChars(serializeSerializable(value.getKey()));
        char[] hexSnapshot = StringUtil.bytesToHexChars(serializeSerializable(value.getStoredSnapshot()));
        String metadata = new StringBuilder(
                hexKey.length + 1 + hexSnapshot.length + 1 + value.getRole().length()).append(hexKey)
                        .append(':').append(hexSnapshot).append(':').append(value.getRole()).toString();
        coll.setMetadata(metadata);
    }

    return coll;
}