Search in sources :

Example 6 with Equipment

use of org.eclipse.persistence.testing.models.jpa.advanced.Equipment in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testMergeNewObject2.

// bug6180972: Tests calling merge on a new Entity that uses int as its ID, verifying it is set and cached correctly
public void testMergeNewObject2() {
    // create an Equipment
    Equipment equip = new Equipment();
    equip.setDescription("New Equipment");
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        Equipment managedEquip = em.merge(equip);
        assertTrue("merged Equipment doesn't have its ID generated", managedEquip.getId() != 0);
        assertNotNull("merged Equipment cannot be found using find", em.find(Equipment.class, managedEquip.getId()));
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment)

Example 7 with Equipment

use of org.eclipse.persistence.testing.models.jpa.advanced.Equipment in project eclipselink by eclipse-ee4j.

the class AdvancedJPAJunitTest method testBackpointerOnMerge.

/**
 * Tests that backpointers are not changed after a merge operation.
 */
public void testBackpointerOnMerge() {
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        // create a new department
        Department department = new Department();
        department.setName("Football");
        // persist the department
        em.persist(department);
        commitTransaction(em);
        closeEntityManager(em);
        // add equipment to the department
        em = createEntityManager();
        beginTransaction(em);
        Equipment equipment = new Equipment();
        equipment.setDescription("Shields & Dummies");
        department.addEquipment(equipment);
        em.merge(department);
        commitTransaction(em);
        closeEntityManager(em);
        assertTrue(department.getEquipment().get(0) == equipment);
        assertEquals(System.identityHashCode(department.getEquipment().get(0)), System.identityHashCode(equipment));
        assertEquals(department.getId(), equipment.getDepartment().getId());
        assertTrue("The department instance (backpointer) from equipment was modified after merge.", department == equipment.getDepartment());
        assertEquals("The department instance (backpointer) from equipment was modified after merge.", System.identityHashCode(department), System.identityHashCode(equipment.getDepartment()));
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw e;
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment)

Example 8 with Equipment

use of org.eclipse.persistence.testing.models.jpa.advanced.Equipment in project eclipselink by eclipse-ee4j.

the class AdvancedJPAJunitTest method testRemoveDepartmentWithPrivateOwnedEquipment.

/**
 * Tests a @PrivateOwned @OneToMany mapping.
 */
public void testRemoveDepartmentWithPrivateOwnedEquipment() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Department department = em.find(Department.class, deptId);
        if (department == null) {
            fail("Department with id=" + deptId + ", was not found.");
        } else {
            Collection<Equipment> equipment = department.getEquipment().values();
            if (equipment.isEmpty()) {
                fail("Department with id=" + deptId + ", did not have any equipment.");
            } else {
                em.remove(department);
                commitTransaction(em);
                assertNull("Department itself was not removed.", em.find(Department.class, deptId));
                for (Equipment e : equipment) {
                    assertNull("New equipment was not deleted.", em.find(Equipment.class, e.getId()));
                }
            }
        }
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        // Re-throw exception to ensure stacktrace appears in test result.
        throw e;
    }
    closeEntityManager(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment)

Example 9 with Equipment

use of org.eclipse.persistence.testing.models.jpa.advanced.Equipment in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testWeaving.

/**
 * Test that all of the classes in the advanced model were weaved as expected.
 */
public void testWeaving() {
    // Only test if weaving was on, test runs without weaving must set this system property.
    if (isWeavingEnabled()) {
        internalTestWeaving(new Employee(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), true);
        internalTestWeaving(new FormerEmployment(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Address(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new PhoneNumber(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new EmploymentPeriod(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        // field-locking
        internalTestWeaving(new Buyer(), false, isWeavingForFetchGroupsEnabled(), false);
        // field-locking
        internalTestWeaving(new GoldBuyer(), false, isWeavingForFetchGroupsEnabled(), false);
        // field-locking
        internalTestWeaving(new PlatinumBuyer(), false, isWeavingForFetchGroupsEnabled(), false);
        // eager 1-m
        internalTestWeaving(new Department(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Golfer(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new GolferPK(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new SmallProject(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new LargeProject(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Man(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Woman(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        // serialized
        internalTestWeaving(new Vegetable(), false, isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new VegetablePK(), false, isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new WorldRank(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Equipment(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new EquipmentCode(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new PartnerLink(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
    }
}
Also used : EmploymentPeriod(org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) VegetablePK(org.eclipse.persistence.testing.models.jpa.advanced.VegetablePK) WorldRank(org.eclipse.persistence.testing.models.jpa.advanced.WorldRank) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) EquipmentCode(org.eclipse.persistence.testing.models.jpa.advanced.EquipmentCode) FormerEmployment(org.eclipse.persistence.testing.models.jpa.advanced.FormerEmployment) GoldBuyer(org.eclipse.persistence.testing.models.jpa.advanced.GoldBuyer) PlatinumBuyer(org.eclipse.persistence.testing.models.jpa.advanced.PlatinumBuyer) Buyer(org.eclipse.persistence.testing.models.jpa.advanced.Buyer) GoldBuyer(org.eclipse.persistence.testing.models.jpa.advanced.GoldBuyer) PlatinumBuyer(org.eclipse.persistence.testing.models.jpa.advanced.PlatinumBuyer) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment) GolferPK(org.eclipse.persistence.testing.models.jpa.advanced.GolferPK) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) Golfer(org.eclipse.persistence.testing.models.jpa.advanced.Golfer) Man(org.eclipse.persistence.testing.models.jpa.advanced.Man) Woman(org.eclipse.persistence.testing.models.jpa.advanced.Woman) PartnerLink(org.eclipse.persistence.testing.models.jpa.advanced.PartnerLink) Vegetable(org.eclipse.persistence.testing.models.jpa.advanced.Vegetable) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 10 with Equipment

use of org.eclipse.persistence.testing.models.jpa.advanced.Equipment in project eclipselink by eclipse-ee4j.

the class AdvancedJPAJunitTest method testMapBuildReferencesPKList.

public void testMapBuildReferencesPKList() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    ClassDescriptor descriptor;
    AbstractSession session = (AbstractSession) JpaHelper.getEntityManager(em).getActiveSession();
    descriptor = session.getDescriptorForAlias("ADV_DEPT");
    Department dept = (Department) em.createQuery("SELECT d FROM ADV_DEPT d WHERE d.equipment IS NOT EMPTY").getResultList().get(0);
    ForeignReferenceMapping mapping = (ForeignReferenceMapping) descriptor.getMappingForAttributeName("equipment");
    Object[] pks = mapping.buildReferencesPKList(dept, mapping.getAttributeValueFromObject(dept), session);
    assertTrue("PK list is of incorrect size.  pks.size: " + pks.length + " expected: " + (dept.getEquipment().size() * 2), pks.length == (dept.getEquipment().size() * 2));
    Map<Integer, Equipment> equipments = (Map<Integer, Equipment>) mapping.valueFromPKList(pks, null, session);
    assertTrue("ValueFromPKList returned list of different size from actual entity.", equipments.size() == dept.getEquipment().size());
    for (Equipment equip : dept.getEquipment().values()) {
        assertTrue("Equipment not found in ValueFromPKList list", equipments.containsKey(equip.getId()));
    }
    rollbackTransaction(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment) Map(java.util.Map) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

Equipment (org.eclipse.persistence.testing.models.jpa.advanced.Equipment)15 EntityManager (jakarta.persistence.EntityManager)12 Department (org.eclipse.persistence.testing.models.jpa.advanced.Department)10 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)7 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)4 EquipmentCode (org.eclipse.persistence.testing.models.jpa.advanced.EquipmentCode)4 UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)3 Map (java.util.Map)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 Address (org.eclipse.persistence.testing.models.jpa.advanced.Address)2 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)2 EmploymentPeriod (org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod)2 FormerEmployment (org.eclipse.persistence.testing.models.jpa.advanced.FormerEmployment)2 LargeProject (org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)2 PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)2 SmallProject (org.eclipse.persistence.testing.models.jpa.advanced.SmallProject)2 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject)2 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1