Example usage for javax.persistence Entity equals

List of usage examples for javax.persistence Entity equals

Introduction

In this page you can find the example usage for javax.persistence Entity equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:com.haulmont.cuba.core.sys.EntityManagerImpl.java

/**
 * Copies all property values from source to dest excluding null values.
 *//*from   www.ja  v a  2  s .  c  om*/
protected void deepCopyIgnoringNulls(Entity source, Entity dest, Set<Entity> visited) {
    if (visited.contains(source))
        return;
    visited.add(source);

    MetadataTools metadataTools = metadata.getTools();
    for (MetaProperty srcProperty : source.getMetaClass().getProperties()) {
        String name = srcProperty.getName();

        if (!PersistenceHelper.isLoaded(source, name)) {
            continue;
        }

        if (srcProperty.isReadOnly()) {
            continue;
        }

        Object value = source.getValue(name);
        if (value == null) {
            continue;
        }

        if (srcProperty.getRange().isClass() && !metadataTools.isEmbedded(srcProperty)) {
            if (!metadataTools.isOwningSide(srcProperty))
                continue;

            Class refClass = srcProperty.getRange().asClass().getJavaClass();
            if (!metadataTools.isPersistent(refClass))
                continue;

            if (srcProperty.getRange().getCardinality().isMany()) {
                if (!metadataTools.isOwningSide(srcProperty))
                    continue;
                //noinspection unchecked
                Collection<Entity> srcCollection = (Collection) value;
                Collection<Entity> dstCollection = dest.getValue(name);
                if (dstCollection == null)
                    throw new RuntimeException("Collection is null: " + srcProperty);
                boolean equal = srcCollection.size() == dstCollection.size();
                if (equal) {
                    if (srcProperty.getRange().isOrdered()) {
                        equal = Arrays.equals(srcCollection.toArray(), dstCollection.toArray());
                    } else {
                        equal = CollectionUtils.isEqualCollection(srcCollection, dstCollection);
                    }
                }
                if (!equal) {
                    dstCollection.clear();
                    for (Entity srcRef : srcCollection) {
                        Entity reloadedRef = findOrCreate(refClass, srcRef.getId());
                        dstCollection.add(reloadedRef);
                        deepCopyIgnoringNulls(srcRef, reloadedRef, visited);
                    }
                }
            } else {
                Entity srcRef = (Entity) value;
                Entity destRef = dest.getValue(name);
                if (srcRef.equals(destRef)) {
                    deepCopyIgnoringNulls(srcRef, destRef, visited);
                } else {
                    Entity reloadedRef = findOrCreate(refClass, srcRef.getId());
                    dest.setValue(name, reloadedRef);
                    deepCopyIgnoringNulls(srcRef, reloadedRef, visited);
                }
            }
        } else if (metadataTools.isEmbedded(srcProperty)) {
            Entity srcRef = (Entity) value;
            Entity destRef = dest.getValue(name);
            if (destRef != null) {
                deepCopyIgnoringNulls(srcRef, destRef, visited);
            } else {
                Entity newRef = (Entity) metadata.create(srcProperty.getRange().asClass().getJavaClass());
                dest.setValue(name, newRef);
                deepCopyIgnoringNulls(srcRef, newRef, visited);
            }
        } else {
            dest.setValue(name, value);
        }
    }
}