Search in sources :

Example 1 with PhoneNumber

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

the class FetchGroupTrackerWeavingTests method verifyCheckFetchedForSetWithFetchGroup_OneToMany.

@Test
public void verifyCheckFetchedForSetWithFetchGroup_OneToMany() {
    Employee emp = new Employee();
    TestFetchGroup fg = new TestFetchGroup();
    ((FetchGroupTracker) emp)._persistence_setFetchGroup(fg);
    assertNull(this.checkAttribute);
    assertNull(this.checkForSetAttribute);
    emp.setPhoneNumbers(new ArrayList<PhoneNumber>());
    assertNull(this.checkAttribute);
    assertNotNull(this.checkForSetAttribute);
    assertEquals("phoneNumbers", this.checkForSetAttribute);
}
Also used : FetchGroupTracker(org.eclipse.persistence.queries.FetchGroupTracker) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) PhoneNumber(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber) Test(org.junit.Test)

Example 2 with PhoneNumber

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

the class NestedDefaultFetchGroupTests method allPhone.

@Test
public void allPhone() {
    EntityManager em = createEntityManager("fieldaccess");
    List<PhoneNumber> allPhones = em.createQuery("SELECT p FROM PhoneNumber p", PhoneNumber.class).getResultList();
    for (PhoneNumber phone : allPhones) {
        assertFetched(phone, defaultPhoneFG);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) PhoneNumber(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber) Test(org.junit.Test)

Example 3 with PhoneNumber

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

the class NestedDefaultFetchGroupTests method internalFindMinEmployee.

void internalFindMinEmployee(boolean loadAddress, boolean loadPhones, boolean useLoadGroup) {
    EntityManager em = createEntityManager("fieldaccess");
    beginTransaction(em);
    int minId = minEmployeeIdWithAddressAndPhones(em);
    assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
    boolean load = false;
    boolean originalLoad = false;
    if (!useLoadGroup) {
        assertTrue(loadAddress == loadPhones);
        load = loadAddress;
        originalLoad = defaultEmployeeFG.shouldLoad();
        if (load != originalLoad) {
            defaultEmployeeFG.setShouldLoad(load);
        }
    }
    try {
        Employee emp;
        if (useLoadGroup) {
            LoadGroup lg = defaultEmployeeFG.toLoadGroup();
            if (!loadAddress) {
                lg.removeAttribute("address");
            }
            if (!loadPhones) {
                lg.removeAttribute("phoneNumbers");
            }
            HashMap<String, Object> hints = new HashMap(1);
            hints.put(QueryHints.LOAD_GROUP, lg);
            emp = em.find(Employee.class, minId, hints);
        } else {
            emp = em.find(Employee.class, minId);
        }
        assertNotNull(emp);
        int nExpected = 2;
        if (loadAddress) {
            nExpected++;
        }
        if (loadPhones) {
            nExpected++;
        }
        assertEquals(nExpected, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
        boolean addressInstantiated = ((ValueHolderInterface) employeeDescriptor.getMappingForAttributeName("address").getAttributeValueFromObject(emp)).isInstantiated();
        assertTrue(loadAddress == addressInstantiated);
        boolean phonesInstantiated = ((IndirectCollection) employeeDescriptor.getMappingForAttributeName("phoneNumbers").getAttributeValueFromObject(emp)).isInstantiated();
        assertTrue(loadPhones == phonesInstantiated);
        emp.getAddress();
        emp.getPhoneNumbers().size();
        assertEquals(4, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
        assertFetched(emp, defaultEmployeeFG);
        assertFetchedAttribute(emp, "address");
        assertFetchedAttribute(emp, "phoneNumbers");
        // Check Address
        FetchGroup fgAddress = defaultEmployeeFG.getGroup("address");
        assertFetched(emp.getAddress(), fgAddress);
        // Check phones
        FetchGroup fgPhones = defaultEmployeeFG.getGroup("phoneNumbers");
        for (PhoneNumber phone : emp.getPhoneNumbers()) {
            assertFetched(phone, fgPhones);
        }
    } finally {
        if (!useLoadGroup) {
            if (load != originalLoad) {
                defaultEmployeeFG.setShouldLoad(originalLoad);
            }
        }
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : HashMap(java.util.HashMap) LoadGroup(org.eclipse.persistence.queries.LoadGroup) IndirectCollection(org.eclipse.persistence.indirection.IndirectCollection) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) ValueHolderInterface(org.eclipse.persistence.indirection.ValueHolderInterface) FetchGroup(org.eclipse.persistence.queries.FetchGroup) PhoneNumber(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber)

Example 4 with PhoneNumber

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

the class NestedFetchGroupTests method dynamicFetchGroup_EmployeeAddressNullPhone.

@Test
public void dynamicFetchGroup_EmployeeAddressNullPhone() throws Exception {
    EntityManager em = createEntityManager("fieldaccess");
    try {
        beginTransaction(em);
        Query query = em.createQuery("SELECT e FROM Employee e WHERE e.gender = :GENDER");
        query.setParameter("GENDER", Gender.Male);
        // Define the fields to be fetched on Employee
        FetchGroup empGroup = new FetchGroup();
        empGroup.addAttribute("firstName");
        empGroup.addAttribute("lastName");
        empGroup.addAttribute("address");
        empGroup.addAttribute("address.city");
        empGroup.addAttribute("address.postalCode");
        // empGroup.addAttribute("phoneNumbers").setUseDefaultFetchGroup(false);
        FetchGroup fullPhone = this.phoneDescriptor.getFetchGroupManager().createFullFetchGroup();
        // to preclude Employee from being loaded by phoneNumber.owner add it to the fetch group
        fullPhone.addAttribute("owner.id");
        empGroup.addAttribute("phoneNumbers", fullPhone);
        // Configure the dynamic FetchGroup
        query.setHint(QueryHints.FETCH_GROUP, empGroup);
        List<Employee> emps = query.getResultList();
        assertNotNull(emps);
        for (Employee emp : emps) {
            FetchGroupTracker tracker = (FetchGroupTracker) emp;
            assertNotNull(tracker._persistence_getFetchGroup());
            // Verify specified fields plus mandatory ones are loaded
            assertTrue(tracker._persistence_isAttributeFetched("id"));
            assertTrue(tracker._persistence_isAttributeFetched("firstName"));
            assertTrue(tracker._persistence_isAttributeFetched("lastName"));
            assertTrue(tracker._persistence_isAttributeFetched("version"));
            // Verify the other fields are not loaded
            assertFalse(tracker._persistence_isAttributeFetched("salary"));
            assertFalse(tracker._persistence_isAttributeFetched("startTime"));
            assertFalse(tracker._persistence_isAttributeFetched("endTime"));
            // Force the loading of lazy fields and verify
            emp.getSalary();
            assertTrue(tracker._persistence_isAttributeFetched("salary"));
            assertTrue(tracker._persistence_isAttributeFetched("startTime"));
            assertTrue(tracker._persistence_isAttributeFetched("endTime"));
            // Now we'll check the address uses the provided dynamic fetch-group
            FetchGroupTracker addrTracker = (FetchGroupTracker) emp.getAddress();
            assertNotNull("Address does not have a FetchGroup", addrTracker._persistence_getFetchGroup());
            assertTrue(addrTracker._persistence_isAttributeFetched("city"));
            assertTrue(addrTracker._persistence_isAttributeFetched("postalCode"));
            assertFalse(addrTracker._persistence_isAttributeFetched("street"));
            assertFalse(addrTracker._persistence_isAttributeFetched("country"));
            // Now we'll check the phoneNumbers use of the default fetch group
            for (PhoneNumber phone : emp.getPhoneNumbers()) {
                FetchGroupTracker phoneTracker = (FetchGroupTracker) phone;
                assertNull("PhoneNumber has a FetchGroup", phoneTracker._persistence_getFetchGroup());
            }
        }
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : FetchGroupTracker(org.eclipse.persistence.queries.FetchGroupTracker) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) Query(jakarta.persistence.Query) FetchGroup(org.eclipse.persistence.queries.FetchGroup) EntityFetchGroup(org.eclipse.persistence.internal.queries.EntityFetchGroup) PhoneNumber(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber) Test(org.junit.Test)

Example 5 with PhoneNumber

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

the class NestedFetchGroupTests method internal_dynamicFetchGroup_EmployeeAddressEmptyPhone.

void internal_dynamicFetchGroup_EmployeeAddressEmptyPhone(boolean shouldLoad) {
    EntityManager em = createEntityManager("fieldaccess");
    try {
        beginTransaction(em);
        Query query = em.createQuery("SELECT e FROM Employee e WHERE e.gender = :GENDER");
        query.setParameter("GENDER", Gender.Male);
        // Define the fields to be fetched on Employee
        FetchGroup fg = new FetchGroup();
        fg.addAttribute("firstName");
        fg.addAttribute("lastName");
        fg.addAttribute("address.city");
        fg.addAttribute("address.postalCode");
        // to preclude Employee from being loaded by phoneNumber.owner add it to the fetch group
        FetchGroup ownerId = new FetchGroup();
        ownerId.addAttribute("owner.id");
        fg.addAttribute("phoneNumbers", ownerId);
        if (shouldLoad) {
            fg.setShouldLoad(true);
        }
        // Configure the dynamic FetchGroup
        query.setHint(QueryHints.FETCH_GROUP, fg);
        List<Employee> emps = query.getResultList();
        assertNotNull(emps);
        assertEquals(1 + (shouldLoad ? 0 : (emps.size() * 2)), getQuerySQLTracker(em).getTotalSQLSELECTCalls());
        for (Employee emp : emps) {
            FetchGroupTracker tracker = (FetchGroupTracker) emp;
            assertNotNull(tracker._persistence_getFetchGroup());
            // Verify specified fields plus mandatory ones are loaded
            assertTrue(tracker._persistence_isAttributeFetched("id"));
            assertTrue(tracker._persistence_isAttributeFetched("firstName"));
            assertTrue(tracker._persistence_isAttributeFetched("lastName"));
            assertTrue(tracker._persistence_isAttributeFetched("version"));
            // Verify the other fields are not loaded
            assertFalse(tracker._persistence_isAttributeFetched("salary"));
            assertFalse(tracker._persistence_isAttributeFetched("startTime"));
            assertFalse(tracker._persistence_isAttributeFetched("endTime"));
            // Force the loading of lazy fields and verify
            emp.getSalary();
            assertTrue(tracker._persistence_isAttributeFetched("salary"));
            assertTrue(tracker._persistence_isAttributeFetched("startTime"));
            assertTrue(tracker._persistence_isAttributeFetched("endTime"));
            // Now we'll check the address uses the provided dynamic fetch-group
            FetchGroupTracker addrTracker = (FetchGroupTracker) emp.getAddress();
            assertNotNull("Address does not have a FetchGroup", addrTracker._persistence_getFetchGroup());
            assertTrue(addrTracker._persistence_isAttributeFetched("city"));
            assertTrue(addrTracker._persistence_isAttributeFetched("postalCode"));
            assertFalse(addrTracker._persistence_isAttributeFetched("street"));
            assertFalse(addrTracker._persistence_isAttributeFetched("country"));
            // Now we'll check the phoneNumbers use of the default fetch group
            for (PhoneNumber phone : emp.getPhoneNumbers()) {
                FetchGroupTracker phoneTracker = (FetchGroupTracker) phone;
                assertNotNull("PhoneNumber does not have a FetchGroup", phoneTracker._persistence_getFetchGroup());
                assertFalse(phoneTracker._persistence_isAttributeFetched("number"));
                assertFalse(phoneTracker._persistence_isAttributeFetched("areaCode"));
                phone.getNumber();
                assertTrue(phoneTracker._persistence_isAttributeFetched("number"));
                assertTrue(phoneTracker._persistence_isAttributeFetched("areaCode"));
            }
        }
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : FetchGroupTracker(org.eclipse.persistence.queries.FetchGroupTracker) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) Query(jakarta.persistence.Query) FetchGroup(org.eclipse.persistence.queries.FetchGroup) EntityFetchGroup(org.eclipse.persistence.internal.queries.EntityFetchGroup) PhoneNumber(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber)

Aggregations

PhoneNumber (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber)61 Employee (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee)60 EntityManager (jakarta.persistence.EntityManager)58 Test (org.junit.Test)43 Query (jakarta.persistence.Query)42 FetchGroup (org.eclipse.persistence.queries.FetchGroup)35 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)19 EntityFetchGroup (org.eclipse.persistence.internal.queries.EntityFetchGroup)17 TypedQuery (jakarta.persistence.TypedQuery)14 FetchGroupTracker (org.eclipse.persistence.queries.FetchGroupTracker)10 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)5 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)5 Address (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address)5 Project (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project)5 List (java.util.List)4 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)3 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)3 Department (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Department)3