Search in sources :

Example 21 with CopyGroup

use of org.eclipse.persistence.sessions.CopyGroup in project eclipselink by eclipse-ee4j.

the class AttributeGroup method toCopyGroup.

/**
 * INTERNAL:
 *    This method is used internally when converting to a copy group.
 */
public CopyGroup toCopyGroup(Map<AttributeGroup, CopyGroup> cloneMap, Map copies) {
    CopyGroup clone = cloneMap.get(this);
    if (clone != null) {
        return clone;
    }
    clone = new CopyGroup(this.name);
    clone.cascadeTree();
    clone.setCopies(copies);
    clone.type = this.type;
    clone.typeName = this.typeName;
    clone.isValidated = this.isValidated;
    cloneMap.put(this, clone);
    if (this.allsubclasses != null) {
        for (CoreAttributeGroup group : this.allsubclasses.values()) {
            clone.getSubClassGroups().put(group.getType(), ((AttributeGroup) group).toCopyGroup(cloneMap, copies));
        }
    }
    if (this.superClassGroup != null) {
        clone.superClassGroup = ((AttributeGroup) this.superClassGroup).toCopyGroup(cloneMap, copies);
    }
    if (this.subClasses != null) {
        clone.subClasses = new HashSet<>();
        for (CoreAttributeGroup group : this.subClasses) {
            clone.subClasses.add(((AttributeGroup) group).toCopyGroup(cloneMap, copies));
        }
    }
    // all attributes and nested groups should be cloned, too
    clone.items = null;
    if (hasItems()) {
        clone.items = new HashMap<>();
        for (AttributeItem item : this.items.values()) {
            clone.items.put(item.getAttributeName(), item.toCopyGroup(cloneMap, clone, copies));
        }
    }
    return clone;
}
Also used : CoreAttributeGroup(org.eclipse.persistence.core.queries.CoreAttributeGroup) AttributeItem(org.eclipse.persistence.internal.queries.AttributeItem) CopyGroup(org.eclipse.persistence.sessions.CopyGroup)

Example 22 with CopyGroup

use of org.eclipse.persistence.sessions.CopyGroup in project eclipselink by eclipse-ee4j.

the class AdvancedCompositePKJunitTest method testCopyAggregateCollection.

// Bug 406957 - Copy fails on AggregateCollectionMapping and on map with @MapKeyColumn
public void testCopyAggregateCollection() {
    Department department = new Department();
    department.setName("DEPT AggregateCollection");
    department.setRole("ROLE AggregateCollection");
    department.setLocation("LOCATION AggregateCollection");
    Competency competency = new Competency();
    competency.description = "Manage groups";
    competency.rating = 9;
    department.addCompetency(competency);
    EntityManager em = createEntityManager();
    CopyGroup privatelyOwned = new CopyGroup();
    Department departmentCopy = (Department) JpaHelper.getEntityManager(em).copy(department, privatelyOwned);
    if (departmentCopy.getCompetencies().size() != department.getCompetencies().size()) {
        fail("departmentCopy.getCompetencies().size() = " + departmentCopy.getCompetencies().size() + "; " + department.getCompetencies().size() + " was expected");
    }
    if (departmentCopy.getCompetencies() == department.getCompetencies()) {
        fail("departmentCopy.getCompetencies() == department.getCompetencies()");
    }
    Competency copmetencyCopy = departmentCopy.getCompetencies().iterator().next();
    if (!competency.description.equals(copmetencyCopy.description)) {
        fail("competency.descripton = " + competency.description + "; but copmetencyCopy.description = " + copmetencyCopy.description);
    }
}
Also used : Department(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department) EntityManager(jakarta.persistence.EntityManager) Competency(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Competency) CopyGroup(org.eclipse.persistence.sessions.CopyGroup)

Example 23 with CopyGroup

use of org.eclipse.persistence.sessions.CopyGroup in project eclipselink by eclipse-ee4j.

the class AdvancedJunitTest method testLazyToInterface.

/**
 * Test that instantiating a lazy relationship to an interface works.
 * This requires that the weaving indirection policy find to real field correctly.
 */
public void testLazyToInterface() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    Parent parent = null;
    Child child = null;
    try {
        parent = new Parent(false);
        child = parent.getChildren().get(0);
        em.persist(parent);
        commitTransaction(em);
    } finally {
        closeEntityManagerAndTransaction(em);
    }
    clearCache();
    em = createEntityManager();
    beginTransaction(em);
    try {
        child = em.find(Child.class, child.getId());
        ((JpaEntityManager) em.getDelegate()).copy(child, new CopyGroup());
        commitTransaction(em);
    } finally {
        closeEntityManagerAndTransaction(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Parent(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Parent) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) CopyGroup(org.eclipse.persistence.sessions.CopyGroup) Child(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Child)

Example 24 with CopyGroup

use of org.eclipse.persistence.sessions.CopyGroup in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testObjectReferencedInBothEmAndSharedCache_ObjectReferenceMappingVH.

// Bug 336280 - Same object referenced from both EM cache and shared cache
public void testObjectReferencedInBothEmAndSharedCache_ObjectReferenceMappingVH() {
    EntityManager em = createEntityManager();
    Employee emp = new Employee();
    emp.setFirstName("Manager");
    Employee emp1 = new Employee();
    emp1.setFirstName("1");
    emp.addManagedEmployee(emp1);
    Employee emp2 = new Employee();
    emp2.setFirstName("2");
    emp.addManagedEmployee(emp2);
    AbstractSession ss = null;
    beginTransaction(em);
    em.persist(emp);
    // in JTA case transaction required to obtain ServerSession through getServersession method.
    ss = getDatabaseSession();
    commitTransaction(em);
    CopyGroup copyGroup = new CopyGroup();
    copyGroup.cascadeAllParts();
    ss.copy(emp, copyGroup);
    Set originalObjects = copyGroup.getCopies().keySet();
    // copyGroup cascades through all mappings.
    // originalObjects should consist of just three objects: emp, emp1, emp2.
    // However if manager_vh is wrapped around manager instance from the shared cache (empShared),
    // the size will be 6: emp, emp1, emp2 and empShared, emp1Shared, emp2Shared.
    assertTrue(originalObjects.size() == 3);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) Set(java.util.Set) ObjectChangeSet(org.eclipse.persistence.sessions.changesets.ObjectChangeSet) HashSet(java.util.HashSet) UnitOfWorkChangeSet(org.eclipse.persistence.sessions.changesets.UnitOfWorkChangeSet) CopyGroup(org.eclipse.persistence.sessions.CopyGroup) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

CopyGroup (org.eclipse.persistence.sessions.CopyGroup)24 EntityManager (jakarta.persistence.EntityManager)20 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)18 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)8 Set (java.util.Set)7 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)7 UnitOfWorkChangeSet (org.eclipse.persistence.sessions.changesets.UnitOfWorkChangeSet)6 Employee (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee)6 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)5 ObjectChangeSet (org.eclipse.persistence.internal.sessions.ObjectChangeSet)5 Map (java.util.Map)4 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)4 AttributeGroup (org.eclipse.persistence.queries.AttributeGroup)4 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Vector (java.util.Vector)3 IdentityHashSet (org.eclipse.persistence.internal.helper.IdentityHashSet)3 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)3