List of usage examples for javax.persistence Entity equals
public boolean equals(Object obj)
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); } } }