Example usage for org.hibernate.type Type replace

List of usage examples for org.hibernate.type Type replace

Introduction

In this page you can find the example usage for org.hibernate.type Type replace.

Prototype

Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner,
        Map copyCache) throws HibernateException;

Source Link

Document

During merge, replace the existing (target) value in the entity we are merging to with a new (original) value from the detached entity we are merging.

Usage

From source file:org.babyfish.hibernate.collection.type.AbstractMACollectionType.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object onReplaceElements(Object original, Object target, CollectionPersister persister, Object owner,
        Map copyCache, SessionImplementor session) {
    // Must before result.clear()
    Iterator itr = this.getClonedIterator((java.util.Collection) original);

    /*//from  w ww  .j a  v a 2  s  .  co m
     *  TODO:
     *  This code is copy from hibernate's CollectionType class.
     *  In its source code, hibernate's author have wrote another 
     *  _TODO_ comment "does not work for EntityMode.DOM4J yet!"
     *  So here, I must check the source code of newest version 
     *  hibernate to make sure whether these code should be changed. 
     */
    java.util.Collection result = (java.util.Collection) target;
    result.clear();

    // copy elements into newly empty target collection
    Type elemType = persister.getElementType();
    while (itr.hasNext()) {
        result.add(elemType.replace(itr.next(), null, session, owner, copyCache));
    }

    // if the original is a PersistentCollection, and that original
    // was not flagged as dirty, then reset the target's dirty flag
    // here after the copy operation.
    // </p>
    // One thing to be careful of here is a "bare" original collection
    // in which case we should never ever ever reset the dirty flag
    // on the target because we simply do not know...
    if (original instanceof PersistentCollection) {
        if (result instanceof PersistentCollection) {
            if (!((PersistentCollection) original).isDirty()) {
                ((PersistentCollection) result).clearDirty();
            }
        }
    }

    return result;
}