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

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

Introduction

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

Prototype

void dirty();

Source Link

Document

Mark the collection as dirty

Usage

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

License:Open Source License

protected PersistentCollection newHibernateCollection(AbstractExternalizablePersistentCollection value,
        Property field) {/*from  w w w  .j a  v  a 2 s . c  om*/
    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;
}

From source file:org.granite.hibernate4.jmf.AbstractPersistentCollectionCodec.java

License:Open Source License

@SuppressWarnings("unchecked")
public void decode(ExtendedObjectInput in, Object v)
        throws IOException, ClassNotFoundException, IllegalAccessException {
    PersistentCollection collection = (PersistentCollection) v;
    if (collection.wasInitialized()) {
        boolean sorted = (collection instanceof SortedSet || collection instanceof SortedMap);
        PersistentCollectionSnapshot snapshot = new JMFPersistentCollectionSnapshot(sorted, null);
        snapshot.readCoreData(in);//from  w  w  w . j  a v a  2 s.com

        if (collection instanceof Map)
            ((Map<Object, Object>) collection).putAll(snapshot.getElementsAsMap());
        else
            ((Collection<Object>) collection).addAll(snapshot.getElementsAsCollection());

        if (snapshot.isDirty())
            collection.dirty();
        else
            collection.clearDirty();
    }
}