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

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

Introduction

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

Prototype

void setSnapshot(Serializable key, String role, Serializable snapshot);

Source Link

Document

After flushing, re-init snapshot state.

Usage

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

License:Open Source License

protected PersistentCollection newHibernateCollection(AbstractExternalizablePersistentCollection value,
        Property field) {/*from   w  ww . ja v  a2 s .co  m*/
    final Type target = field.getType();
    final boolean initialized = value.isInitialized();
    final String metadata = value.getMetadata();
    final boolean dirty = value.isDirty();
    final boolean sorted = (SortedSet.class.isAssignableFrom(TypeUtil.classOfType(target))
            || SortedMap.class.isAssignableFrom(TypeUtil.classOfType(target)));

    Comparator<?> comparator = null;
    if (sorted && field.isAnnotationPresent(Sort.class)) {
        Sort sort = field.getAnnotation(Sort.class);
        if (sort.type() == SortType.COMPARATOR) {
            try {
                comparator = TypeUtil.newInstance(sort.comparator(), Comparator.class);
            } catch (Exception e) {
                throw new RuntimeException("Could not create instance of Comparator: " + sort.comparator());
            }
        }
    }

    PersistentCollection coll = null;
    if (value instanceof ExternalizablePersistentSet) {
        if (initialized) {
            Set<?> set = ((ExternalizablePersistentSet) value).getContentAsSet(target, comparator);
            coll = (sorted ? new PersistentSortedSet(null, (SortedSet<?>) set) : new PersistentSet(null, set));
        } else
            coll = (sorted ? new PersistentSortedSet() : new PersistentSet());
    } else if (value instanceof ExternalizablePersistentBag) {
        if (initialized) {
            List<?> bag = ((ExternalizablePersistentBag) value).getContentAsList(target);
            coll = new PersistentBag(null, bag);
        } else
            coll = new PersistentBag();
    } else if (value instanceof ExternalizablePersistentList) {
        if (initialized) {
            List<?> list = ((ExternalizablePersistentList) value).getContentAsList(target);
            coll = new PersistentList(null, list);
        } else
            coll = new PersistentList();
    } else if (value instanceof ExternalizablePersistentMap) {
        if (initialized) {
            Map<?, ?> map = ((ExternalizablePersistentMap) value).getContentAsMap(target, comparator);
            coll = (sorted ? new PersistentSortedMap(null, (SortedMap<?, ?>) map)
                    : new PersistentMap(null, map));
        } else
            coll = (sorted ? new PersistentSortedMap() : new PersistentMap());
    } else
        throw new RuntimeException("Illegal externalizable persitent class: " + value);

    if (metadata != null && serializeMetadata != SerializeMetadata.NO
            && (serializeMetadata == SerializeMetadata.YES || !initialized)) {
        String[] toks = metadata.split(":", 3);
        if (toks.length != 3)
            throw new RuntimeException("Invalid collection metadata: " + metadata);
        Serializable key = deserializeSerializable(StringUtil.hexStringToBytes(toks[0]));
        Serializable snapshot = deserializeSerializable(StringUtil.hexStringToBytes(toks[1]));
        String role = toks[2];
        coll.setSnapshot(key, role, snapshot);
    }

    if (initialized && dirty)
        coll.dirty();

    return coll;
}