Search in sources :

Example 1 with Address

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

the class NestedDefaultFetchGroupTests method allAddress.

/*    void internalFindMinEmployee(boolean loadAddress, boolean loadPhones) {
        EntityManager em = createEntityManager();
        int minId = minEmployeeIdWithAddressAndPhones(em);
        assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());

        FetchGroup fg = employeeDescriptor.getFetchGroupManager().getDefaultFetchGroup();
        FetchGroup phonesFg = fg.getGroup("phoneNumbers");

        FetchGroup fgAddress = fg.getGroup("address");
        boolean originalLoadAddress = fgAddress.shouldLoad();
        if(originalLoadAddress != loadAddress) {
            fgAddress.setShouldLoad(loadAddress);
        }

        FetchGroup fgPhones = fg.getGroup("phoneNumbers");
        boolean originalLoadPhones = fgPhones.shouldLoad();
        if(originalLoadPhones != loadPhones) {
            fgPhones.setShouldLoad(loadPhones);
        }

        try {
            Employee 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)((ForeignReferenceMapping)employeeDescriptor.getMappingForAttributeName("address")).getAttributeValueFromObject(emp)).isInstantiated();
            boolean phonesInstantiated = ((IndirectCollection)((ForeignReferenceMapping)employeeDescriptor.getMappingForAttributeName("phoneNumbers")).getAttributeValueFromObject(emp)).isInstantiated();

            if(loadAddress) {
                assertTrue(addressInstantiated);
            }
            if(loadPhones) {
                assertTrue(phonesInstantiated);
            }

            emp.getAddress();
            emp.getPhoneNumbers().size();

            assertEquals(4, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
            assertFetched(emp, defaultEmployeeFG);
            assertFetchedAttribute(emp, "address");
            assertFetchedAttribute(emp, "phoneNumbers");

            // Check Address
            assertFetched(emp.getAddress(), fgAddress);

            // Check phones
            for (PhoneNumber phone: emp.getPhoneNumbers()) {
                assertFetched(phone, fgPhones);
            }
        } finally {
            if(originalLoadAddress != loadAddress) {
                fgAddress.setShouldLoad(originalLoadAddress);
            }
            if(originalLoadPhones != loadPhones) {
                fgPhones.setShouldLoad(originalLoadPhones);
            }
        }
    }*/
@Test
public void allAddress() {
    EntityManager em = createEntityManager("fieldaccess");
    List<Address> allAddresses = em.createQuery("SELECT a FROM Address a", Address.class).getResultList();
    for (Address address : allAddresses) {
        assertNoFetchGroup(address);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Address(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address) Test(org.junit.Test)

Example 2 with Address

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

the class NestedFetchGroupTests method allNestedFetchGroupWithJoinFetch.

@Test
public void allNestedFetchGroupWithJoinFetch() {
    EntityManager em = createEntityManager("fieldaccess");
    try {
        beginTransaction(em);
        // select employees who are neither managers nor team leaders
        Query query = em.createQuery("SELECT e FROM Employee e WHERE NOT EXISTS(SELECT p.id FROM Project p WHERE p.teamLeader = e) AND NOT EXISTS(SELECT e2.id FROM Employee e2 WHERE e2.manager = e)");
        FetchGroup employeeFG = new FetchGroup("employee");
        employeeFG.addAttribute("lastName");
        employeeFG.addAttribute("address.country");
        employeeFG.addAttribute("address.city");
        query.setHint(QueryHints.LEFT_FETCH, "e.address");
        employeeFG.addAttribute("phoneNumbers");
        query.setHint(QueryHints.LEFT_FETCH, "e.phoneNumbers");
        employeeFG.addAttribute("projects.name");
        employeeFG.addAttribute("projects.teamLeader.firstName");
        // employeeFG.addAttribute("projects.teamLeader.address.street");
        // employeeFG.addAttribute("projects.teamLeader.address.postalCode");
        employeeFG.addAttribute("projects.teamLeader.phoneNumbers.owner");
        employeeFG.addAttribute("projects.teamLeader.phoneNumbers.type");
        employeeFG.addAttribute("projects.teamLeader.phoneNumbers.areaCode");
        query.setHint(QueryHints.LEFT_FETCH, "e.projects.teamLeader.phoneNumbers");
        employeeFG.addAttribute("manager.firstName");
        // employeeFG.addAttribute("manager.address.street");
        // employeeFG.addAttribute("manager.address.postalCode");
        employeeFG.addAttribute("manager.phoneNumbers.owner");
        employeeFG.addAttribute("manager.phoneNumbers.type");
        employeeFG.addAttribute("manager.phoneNumbers.areaCode");
        query.setHint(QueryHints.LEFT_FETCH, "e.manager.phoneNumbers");
        // department attribute defined with JoinFetchType.OUTER
        employeeFG.addAttribute("department.name");
        query.setHint(QueryHints.FETCH_GROUP, employeeFG);
        List<Employee> employees = query.getResultList();
        assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
        for (Employee emp : employees) {
            assertFetched(emp, employeeFG);
            Address address = emp.getAddress();
            if (address != null) {
                assertFetched(address, employeeFG.getGroup("address"));
            }
            for (PhoneNumber phone : emp.getPhoneNumbers()) {
                assertFetched(phone, defaultPhoneFG);
            }
            for (Project project : emp.getProjects()) {
                assertFetched(project, employeeFG.getGroup("projects"));
                Employee teamLeader = project.getTeamLeader();
                if (teamLeader != null) {
                    assertFetched(teamLeader, employeeFG.getGroup("projects.teamLeader"));
                    for (PhoneNumber phone : teamLeader.getPhoneNumbers()) {
                        assertFetched(phone, employeeFG.getGroup("projects.teamLeader.phoneNumbers"));
                    }
                }
            }
            Employee manager = emp.getManager();
            if (manager != null) {
                assertFetched(manager, employeeFG.getGroup("manager"));
                for (PhoneNumber phone : manager.getPhoneNumbers()) {
                    assertFetched(phone, employeeFG.getGroup("manager.phoneNumbers"));
                }
            }
            Department department = emp.getDepartment();
            if (department != null) {
                assertFetched(department, employeeFG.getGroup("department"));
            }
        }
        assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : Project(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project) EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Department) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) Query(jakarta.persistence.Query) Address(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address) 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 3 with Address

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

the class SessionBeanTests method testSetup.

/**
 * The setup is done as a test, both to record its failure, and to allow
 * execution in the server.
 */
public void testSetup() throws Exception {
    new AdvancedTableCreator().replaceTables(JUnitTestCase.getServerSession("sessionbean"));
    Employee bob = new Employee();
    bob.setFirstName("Bob");
    bob.setLastName("Jones");
    bob.setAddress(new Address());
    bob.setDepartment(new Department());
    getEmployeeService().insert(bob);
    Employee joe = new Employee();
    joe.setFirstName("Joe");
    joe.setLastName("Smith");
    joe.setAddress(new Address());
    joe.setDepartment(new Department());
    getEmployeeService().insert(joe);
}
Also used : Department(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Department) AdvancedTableCreator(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.AdvancedTableCreator) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address)

Example 4 with Address

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

the class EntityManagerJUnitTestSuite method testFindDeleteAllPersist.

// test for bug 4755392:
// AFTER DELETEALL OBJECT STILL DEEMED EXISTING
public void testFindDeleteAllPersist() {
    if (getDatabaseSession().getPlatform().isSymfoware()) {
        getDatabaseSession().logMessage("Test testFindDeleteAllPersist skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    }
    String firstName = "testFindDeleteAllPersist";
    // create Employees
    Employee empWithAddress = new Employee();
    empWithAddress.setFirstName(firstName);
    empWithAddress.setLastName("WithAddress");
    empWithAddress.setAddress(new Address());
    Employee empWithoutAddress = new Employee();
    empWithoutAddress.setFirstName(firstName);
    empWithoutAddress.setLastName("WithoutAddress");
    EntityManager em = createEntityManager();
    // make sure no Employee with the specified firstName exists.
    beginTransaction(em);
    try {
        em.createQuery("DELETE FROM Employee e WHERE e.firstName = '" + firstName + "'").executeUpdate();
        commitTransaction(em);
    } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw ex;
    }
    // persist
    beginTransaction(em);
    try {
        em.persist(empWithAddress);
        em.persist(empWithoutAddress);
        commitTransaction(em);
    } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw ex;
    }
    // clear cache
    clearCache();
    em.clear();
    // Find both to bring into the cache, delete empWithoutAddress.
    // Because the address VH is not triggered both objects should be invalidated.
    beginTransaction(em);
    try {
        Employee empWithAddressFound = em.find(Employee.class, empWithAddress.getId());
        empWithAddressFound.toString();
        Employee empWithoutAddressFound = em.find(Employee.class, empWithoutAddress.getId());
        empWithoutAddressFound.toString();
        int nDeleted = em.createQuery("DELETE FROM Employee e WHERE e.firstName = '" + firstName + "' and e.address IS NULL").executeUpdate();
        assertTrue(nDeleted > 0);
        commitTransaction(em);
    } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw ex;
    }
    // we can no longer rely on the query above to clear the Employee from the persistence context.
    // Clearing the context to allow us to proceed.
    em.clear();
    // persist new empWithoutAddress - the one that has been deleted from the db.
    beginTransaction(em);
    try {
        Employee newEmpWithoutAddress = new Employee();
        newEmpWithoutAddress.setFirstName(firstName);
        newEmpWithoutAddress.setLastName("newWithoutAddress");
        newEmpWithoutAddress.setId(empWithoutAddress.getId());
        em.persist(newEmpWithoutAddress);
        commitTransaction(em);
    } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw ex;
    }
    // persist new empWithAddress - the one still in the db.
    beginTransaction(em);
    try {
        Employee newEmpWithAddress = new Employee();
        newEmpWithAddress.setFirstName(firstName);
        newEmpWithAddress.setLastName("newWithAddress");
        newEmpWithAddress.setId(empWithAddress.getId());
        em.persist(newEmpWithAddress);
        fail("EntityExistsException was expected");
    } catch (EntityExistsException ex) {
    // "cant_persist_detatched_object" - ignore the expected exception
    } finally {
        rollbackTransaction(em);
    }
    // clean up
    beginTransaction(em);
    em.createQuery("DELETE FROM Employee e WHERE e.firstName = '" + firstName + "'").executeUpdate();
    commitTransaction(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address) EntityExistsException(jakarta.persistence.EntityExistsException)

Example 5 with Address

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

the class EntityManagerJUnitTestSuite method testCloneable.

// Test the clone method works correctly with lazy attributes.
public void testCloneable() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee employee = new Employee();
        employee.setFirstName("Owen");
        employee.setLastName("Hargreaves");
        employee.getAddress();
        Employee clone = employee.clone();
        Address address = new Address();
        address.setCity("Munich");
        clone.setAddress(address);
        clone.getAddress();
        em.persist(clone);
        if (employee.getAddress() == clone.getAddress()) {
            fail("Changing clone address changed original.");
        }
        commitTransaction(em);
        clearCache();
        closeEntityManager(em);
        em = createEntityManager();
        beginTransaction(em);
        employee = em.find(Employee.class, clone.getId());
        clone = employee.clone();
        address = new Address();
        address.setCity("Not Munich");
        clone.setAddress(address);
        clone.getAddress();
        if (employee.getAddress() == clone.getAddress()) {
            fail("Changing clone address changed original.");
        }
        if (employee.getAddress() == null) {
            fail("Changing clone address reset original to null.");
        }
        if (clone.getAddress() != address) {
            fail("Changing clone did not work.");
        }
        em.remove(employee);
        commitTransaction(em);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address)

Aggregations

Address (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address)27 Employee (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee)22 EntityManager (jakarta.persistence.EntityManager)20 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)14 Query (jakarta.persistence.Query)7 Test (org.junit.Test)6 PhoneNumber (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Department (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Department)4 Project (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project)4 EntityExistsException (jakarta.persistence.EntityExistsException)3 LargeProject (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject)3 SmallProject (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject)3 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SuperLargeProject)3 Iterator (java.util.Iterator)2 ValidationException (org.eclipse.persistence.exceptions.ValidationException)2 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)2 EntityFetchGroup (org.eclipse.persistence.internal.queries.EntityFetchGroup)2 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)2