Search in sources :

Example 1 with IdentityHashSet

use of org.eclipse.persistence.internal.helper.IdentityHashSet in project eclipselink by eclipse-ee4j.

the class SimpleSerializeFetchGroupTests method copyCascade.

void copyCascade(int cascadeDepth) {
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        Query query = em.createQuery("SELECT e FROM Employee e");
        List<Employee> employees = query.getResultList();
        CopyGroup group = new CopyGroup();
        if (cascadeDepth == CopyGroup.NO_CASCADE) {
            group.dontCascade();
        } else if (cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
            // default cascade depth setting
            group.cascadePrivateParts();
        } else if (cascadeDepth == CopyGroup.CASCADE_ALL_PARTS) {
            group.cascadeAllParts();
        } else {
            fail("Invalid cascadeDepth = " + cascadeDepth);
        }
        group.setShouldResetPrimaryKey(true);
        List<Employee> employeesCopy;
        if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
            // In this case the objects should be copied one by one - each one using a new CopyGroup.
            // That ensures most referenced object are original ones (not copies) -
            // the only exception is privately owned objects in CASCADE_PRIVATE_PARTS case.
            employeesCopy = new ArrayList(employees.size());
            for (Employee emp : employees) {
                CopyGroup groupClone = group.clone();
                employeesCopy.add((Employee) em.unwrap(JpaEntityManager.class).copy(emp, groupClone));
            }
        } else {
            // cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
            // In this case all objects should be copied using a single CopyGroup.
            // That ensures identities of the copies:
            // for instance if several employees referenced the same project,
            // then all copies of these employees will reference the single copy of the project.
            employeesCopy = (List) em.unwrap(JpaEntityManager.class).copy(employees, group);
        }
        // IdentityHashSets will be used to verify copy identities
        IdentityHashSet originalEmployees = new IdentityHashSet();
        IdentityHashSet copyEmployees = new IdentityHashSet();
        IdentityHashSet originalAddresses = new IdentityHashSet();
        IdentityHashSet copyAddresses = new IdentityHashSet();
        IdentityHashSet originalProjects = new IdentityHashSet();
        IdentityHashSet copyProjects = new IdentityHashSet();
        IdentityHashSet originalPhones = new IdentityHashSet();
        IdentityHashSet copyPhones = new IdentityHashSet();
        int size = employees.size();
        for (int i = 0; i < size; i++) {
            Employee emp = employees.get(i);
            Employee empCopy = employeesCopy.get(i);
            if (cascadeDepth == CopyGroup.CASCADE_ALL_PARTS) {
                originalEmployees.add(emp);
                copyEmployees.add(empCopy);
            } else {
            // cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS
            // In this case all Employees referenced by empCopyes are originals (manager and managed employees).
            // Therefore if we add here each emp and empCopy to originalEmployees and copyEmployees respectively
            // then copyEmployees will always contain all original managers and managed + plus all copies.
            // Therefore in this case originalEmployees and copyEmployees will contain only references (managers and managed employees).
            }
            if (emp.getAddress() == null) {
                assertTrue("emp.getAddress() == null, but empCopy.getAddress() != null", empCopy.getAddress() == null);
            } else {
                originalAddresses.add(emp.getAddress());
                copyAddresses.add(empCopy.getAddress());
                if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
                    assertTrue("address has been copied", emp.getAddress() == empCopy.getAddress());
                } else {
                    // cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
                    assertFalse("address has not been copied", emp.getAddress() == empCopy.getAddress());
                }
            }
            boolean same;
            for (Project project : emp.getProjects()) {
                originalProjects.add(project);
                same = false;
                for (Project projectCopy : empCopy.getProjects()) {
                    copyProjects.add(projectCopy);
                    if (!same && project == projectCopy) {
                        same = true;
                    }
                }
                if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
                    assertTrue("project has been copied", same);
                } else {
                    // cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
                    assertFalse("project has not been copied", same);
                }
            }
            for (Employee managedEmp : emp.getManagedEmployees()) {
                originalEmployees.add(managedEmp);
                same = false;
                for (Employee managedEmpCopy : empCopy.getManagedEmployees()) {
                    copyEmployees.add(managedEmpCopy);
                    if (!same && managedEmp == managedEmpCopy) {
                        same = true;
                    }
                }
                if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
                    assertTrue("managedEmployee has been copied", same);
                } else {
                    // cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
                    assertFalse("managedEmployee has not been copied", same);
                }
            }
            if (emp.getManager() == null) {
                assertTrue("emp.getManager() == null, but empCopy.getManager() != null", empCopy.getManager() == null);
            } else {
                originalEmployees.add(emp.getManager());
                copyEmployees.add(empCopy.getManager());
                if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
                    assertTrue("manager has been copied", emp.getManager() == empCopy.getManager());
                } else {
                    // cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
                    assertFalse("manager has not been copied", emp.getManager() == empCopy.getManager());
                }
            }
            // phoneNumbers is privately owned
            for (PhoneNumber phone : emp.getPhoneNumbers()) {
                originalPhones.add(phone);
                same = false;
                for (PhoneNumber phoneCopy : empCopy.getPhoneNumbers()) {
                    copyPhones.add(phoneCopy);
                    if (!same && phone == phoneCopy) {
                        same = true;
                    }
                }
                if (cascadeDepth == CopyGroup.NO_CASCADE) {
                    assertTrue("phone has been copied", same);
                } else {
                    // cascadeDepth == CopyGroup.CASCADE_ALL_PARTS || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS
                    assertFalse("phone has not been copied", same);
                }
            }
        }
        assertTrue("copyEmployees.size() == " + copyEmployees.size() + "; was expected " + originalEmployees.size(), originalEmployees.size() == copyEmployees.size());
        assertTrue("copyAddresses.size() == " + copyAddresses.size() + "; was expected " + originalAddresses.size(), originalAddresses.size() == copyAddresses.size());
        assertTrue("copyProjects.size() == " + copyProjects.size() + "; was expected " + originalProjects.size(), originalProjects.size() == copyProjects.size());
        assertTrue("copyPhones.size() == " + copyPhones.size() + "; was expected " + originalPhones.size(), originalPhones.size() == copyPhones.size());
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
    }
}
Also used : IdentityHashSet(org.eclipse.persistence.internal.helper.IdentityHashSet) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) TypedQuery(jakarta.persistence.TypedQuery) Query(jakarta.persistence.Query) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) CopyGroup(org.eclipse.persistence.sessions.CopyGroup) ArrayList(java.util.ArrayList) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)

Example 2 with IdentityHashSet

use of org.eclipse.persistence.internal.helper.IdentityHashSet in project cuba by cuba-platform.

the class EntityStates method checkLoadedWithView.

/**
 * Check that all properties of the view are loaded from DB for the passed entity.
 * Throws exception if some property is not loaded.
 *
 * @param entity entity
 * @param view   view
 * @throws IllegalArgumentException if at least one of properties is not loaded
 */
@SuppressWarnings("unchecked")
public void checkLoadedWithView(Entity entity, View view) {
    checkNotNullArgument(entity);
    checkNotNullArgument(view);
    checkLoadedWithView(entity, view, new IdentityHashSet());
}
Also used : IdentityHashSet(org.eclipse.persistence.internal.helper.IdentityHashSet)

Example 3 with IdentityHashSet

use of org.eclipse.persistence.internal.helper.IdentityHashSet in project cuba by cuba-platform.

the class EntityStates method isLoadedWithView.

/**
 * Check that all properties of the view are loaded from DB for the passed entity.
 *
 * @param entity entity
 * @param view   view name
 * @return false if at least one of properties is not loaded
 */
@SuppressWarnings("unchecked")
public boolean isLoadedWithView(Entity entity, View view) {
    checkNotNullArgument(entity);
    checkNotNullArgument(view);
    return isLoadedWithView(entity, view, new IdentityHashSet());
}
Also used : IdentityHashSet(org.eclipse.persistence.internal.helper.IdentityHashSet)

Example 4 with IdentityHashSet

use of org.eclipse.persistence.internal.helper.IdentityHashSet in project eclipselink by eclipse-ee4j.

the class UnitOfWorkImpl method addPrivateOwnedObject.

/**
 * INTERNAL:
 * Add a privately owned object to the privateOwnedObjectsMap.
 * The UnitOfWork needs to keep track of privately owned objects in order to
 * detect and remove private owned objects which are de-referenced.
 */
public void addPrivateOwnedObject(DatabaseMapping mapping, Object privateOwnedObject) {
    // only allow mapped, non-null objects to be added
    if (privateOwnedObject != null && getDescriptor(privateOwnedObject) != null) {
        Map<DatabaseMapping, Set> privateOwnedObjects = getPrivateOwnedObjects();
        Set objectsForMapping = privateOwnedObjects.get(mapping);
        if (objectsForMapping == null) {
            objectsForMapping = new IdentityHashSet();
            privateOwnedObjects.put(mapping, objectsForMapping);
        }
        objectsForMapping.add(privateOwnedObject);
    }
}
Also used : IdentityHashSet(org.eclipse.persistence.internal.helper.IdentityHashSet) IdentityHashSet(org.eclipse.persistence.internal.helper.IdentityHashSet) Set(java.util.Set) HashSet(java.util.HashSet) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping)

Example 5 with IdentityHashSet

use of org.eclipse.persistence.internal.helper.IdentityHashSet in project eclipselink by eclipse-ee4j.

the class SimpleSerializeFetchGroupTests method copyCascade.

void copyCascade(int cascadeDepth) {
    EntityManager em = createEntityManager("fieldaccess");
    try {
        beginTransaction(em);
        Query query = em.createQuery("SELECT e FROM Employee e");
        List<Employee> employees = query.getResultList();
        CopyGroup group = new CopyGroup();
        if (cascadeDepth == CopyGroup.NO_CASCADE) {
            group.dontCascade();
        } else if (cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
            // default cascade depth setting
            group.cascadePrivateParts();
        } else if (cascadeDepth == CopyGroup.CASCADE_ALL_PARTS) {
            group.cascadeAllParts();
        } else {
            fail("Invalid cascadeDepth = " + cascadeDepth);
        }
        group.setShouldResetPrimaryKey(true);
        List<Employee> employeesCopy;
        if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
            // In this case the objects should be copied one by one - each one using a new CopyGroup.
            // That ensures most referenced object are original ones (not copies) -
            // the only exception is privately owned objects in CASCADE_PRIVATE_PARTS case.
            employeesCopy = new ArrayList(employees.size());
            for (Employee emp : employees) {
                CopyGroup groupClone = group.clone();
                employeesCopy.add((Employee) em.unwrap(JpaEntityManager.class).copy(emp, groupClone));
            }
        } else {
            // cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
            // In this case all objects should be copied using a single CopyGroup.
            // That ensures identities of the copies:
            // for instance if several employees referenced the same project,
            // then all copies of these employees will reference the single copy of the project.
            employeesCopy = (List) em.unwrap(JpaEntityManager.class).copy(employees, group);
        }
        // IdentityHashSets will be used to verify copy identities
        IdentityHashSet originalEmployees = new IdentityHashSet();
        IdentityHashSet copyEmployees = new IdentityHashSet();
        IdentityHashSet originalAddresses = new IdentityHashSet();
        IdentityHashSet copyAddresses = new IdentityHashSet();
        IdentityHashSet originalProjects = new IdentityHashSet();
        IdentityHashSet copyProjects = new IdentityHashSet();
        IdentityHashSet originalPhones = new IdentityHashSet();
        IdentityHashSet copyPhones = new IdentityHashSet();
        int size = employees.size();
        for (int i = 0; i < size; i++) {
            Employee emp = employees.get(i);
            Employee empCopy = employeesCopy.get(i);
            if (cascadeDepth == CopyGroup.CASCADE_ALL_PARTS) {
                originalEmployees.add(emp);
                copyEmployees.add(empCopy);
            } else {
            // cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS
            // In this case all Employees referenced by empCopyes are originals (manager and managed employees).
            // Therefore if we add here each emp and empCopy to originalEmployees and copyEmployees respectively
            // then copyEmployees will always contain all original managers and managed + plus all copies.
            // Therefore in this case originalEmployees and copyEmployees will contain only references (managers and managed employees).
            }
            if (emp.getAddress() == null) {
                assertTrue("emp.getAddress() == null, but empCopy.getAddress() != null", empCopy.getAddress() == null);
            } else {
                originalAddresses.add(emp.getAddress());
                copyAddresses.add(empCopy.getAddress());
                if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
                    assertTrue("address has been copied", emp.getAddress() == empCopy.getAddress());
                } else {
                    // cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
                    assertFalse("address has not been copied", emp.getAddress() == empCopy.getAddress());
                }
            }
            boolean same;
            for (Project project : emp.getProjects()) {
                originalProjects.add(project);
                same = false;
                for (Project projectCopy : empCopy.getProjects()) {
                    copyProjects.add(projectCopy);
                    if (!same && project == projectCopy) {
                        same = true;
                    }
                }
                if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
                    assertTrue("project has been copied", same);
                } else {
                    // cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
                    assertFalse("project has not been copied", same);
                }
            }
            for (Employee managedEmp : emp.getManagedEmployees()) {
                originalEmployees.add(managedEmp);
                same = false;
                for (Employee managedEmpCopy : empCopy.getManagedEmployees()) {
                    copyEmployees.add(managedEmpCopy);
                    if (!same && managedEmp == managedEmpCopy) {
                        same = true;
                    }
                }
                if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
                    assertTrue("managedEmployee has been copied", same);
                } else {
                    // cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
                    assertFalse("managedEmployee has not been copied", same);
                }
            }
            if (emp.getManager() == null) {
                assertTrue("emp.getManager() == null, but empCopy.getManager() != null", empCopy.getManager() == null);
            } else {
                originalEmployees.add(emp.getManager());
                copyEmployees.add(empCopy.getManager());
                if (cascadeDepth == CopyGroup.NO_CASCADE || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS) {
                    assertTrue("manager has been copied", emp.getManager() == empCopy.getManager());
                } else {
                    // cascadeDepth == CopyGroup.CASCADE_ALL_PARTS
                    assertFalse("manager has not been copied", emp.getManager() == empCopy.getManager());
                }
            }
            // phoneNumbers is privately owned
            for (PhoneNumber phone : emp.getPhoneNumbers()) {
                originalPhones.add(phone);
                same = false;
                for (PhoneNumber phoneCopy : empCopy.getPhoneNumbers()) {
                    copyPhones.add(phoneCopy);
                    if (!same && phone == phoneCopy) {
                        same = true;
                    }
                }
                if (cascadeDepth == CopyGroup.NO_CASCADE) {
                    assertTrue("phone has been copied", same);
                } else {
                    // cascadeDepth == CopyGroup.CASCADE_ALL_PARTS || cascadeDepth == CopyGroup.CASCADE_PRIVATE_PARTS
                    assertFalse("phone has not been copied", same);
                }
            }
        }
        assertTrue("copyEmployees.size() == " + copyEmployees.size() + "; was expected " + originalEmployees.size(), originalEmployees.size() == copyEmployees.size());
        assertTrue("copyAddresses.size() == " + copyAddresses.size() + "; was expected " + originalAddresses.size(), originalAddresses.size() == copyAddresses.size());
        assertTrue("copyProjects.size() == " + copyProjects.size() + "; was expected " + originalProjects.size(), originalProjects.size() == copyProjects.size());
        assertTrue("copyPhones.size() == " + copyPhones.size() + "; was expected " + originalPhones.size(), originalPhones.size() == copyPhones.size());
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
    }
}
Also used : IdentityHashSet(org.eclipse.persistence.internal.helper.IdentityHashSet) Project(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) TypedQuery(jakarta.persistence.TypedQuery) Query(jakarta.persistence.Query) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) CopyGroup(org.eclipse.persistence.sessions.CopyGroup) ArrayList(java.util.ArrayList) PhoneNumber(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber)

Aggregations

IdentityHashSet (org.eclipse.persistence.internal.helper.IdentityHashSet)7 EntityManager (jakarta.persistence.EntityManager)4 Query (jakarta.persistence.Query)4 Set (java.util.Set)3 TypedQuery (jakarta.persistence.TypedQuery)2 ArrayList (java.util.ArrayList)2 IdentityHashMap (java.util.IdentityHashMap)2 EntityFetchGroup (org.eclipse.persistence.internal.queries.EntityFetchGroup)2 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)2 FetchGroup (org.eclipse.persistence.queries.FetchGroup)2 CopyGroup (org.eclipse.persistence.sessions.CopyGroup)2 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)2 Employee (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee)2 HashSet (java.util.HashSet)1 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)1 PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)1 Project (org.eclipse.persistence.testing.models.jpa.advanced.Project)1 PhoneNumber (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber)1 Project (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project)1