Search in sources :

Example 16 with Address

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

the class EntityManagerJUnitTestSuite method testSetFieldForPropertyAccessWithNewEM.

/**
 * Bug 801
 * Test to ensure when property access is used and the underlying variable is changed the change
 * is correctly reflected in the database
 *
 * In this test we test making the change when an existing object is read into a new EM
 */
public void testSetFieldForPropertyAccessWithNewEM() {
    EntityManager em = createEntityManager();
    Employee employee = new Employee();
    employee.setFirstName("Andy");
    employee.setLastName("Dufresne");
    Employee manager = new Employee();
    manager.setFirstName("Bobby");
    manager.setLastName("Dufresne");
    employee.setManager(manager);
    Address address = new Address();
    address.setCity("Shawshank");
    employee.setAddress(address);
    beginTransaction(em);
    em.persist(employee);
    commitTransaction(em);
    int id = employee.getId();
    int addressId = address.getID();
    int managerId = manager.getId();
    em = createEntityManager();
    beginTransaction(em);
    employee = em.find(Employee.class, id);
    employee.getAddress();
    employee.getManager();
    address = new Address();
    address.setCity("Metropolis");
    employee.setAddress(address);
    manager = new Employee();
    manager.setFirstName("Metro");
    manager.setLastName("Dufresne");
    employee.setManagerField(manager);
    try {
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw e;
    }
    clearCache();
    em = createEntityManager();
    beginTransaction(em);
    employee = em.find(Employee.class, id);
    address = employee.getAddress();
    manager = employee.getManager();
    assertTrue("The address was not persisted.", employee.getAddress() != null);
    assertTrue("The address was not correctly persisted.", employee.getAddress().getCity().equals("Metropolis"));
    assertTrue("The manager was not persisted.", employee.getManager() != null);
    assertTrue("The manager was not correctly persisted.", employee.getManager().getFirstName().equals("Metro"));
    Address initialAddress = em.find(Address.class, addressId);
    Employee initialManager = em.find(Employee.class, managerId);
    employee.setAddress((Address) null);
    employee.setManager(null);
    em.remove(address);
    em.remove(employee);
    em.remove(manager);
    em.remove(initialAddress);
    em.remove(initialManager);
    commitTransaction(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address)

Example 17 with Address

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

the class EntityManagerJUnitTestSuite method testNoPersistOnCommitProperties.

// Test Not using the persist operation on commit.
public void testNoPersistOnCommitProperties() {
    // Properties only works in jse.
    if (isOnServer()) {
        return;
    }
    Map properties = new HashMap();
    properties.putAll(JUnitTestCaseHelper.getDatabaseProperties());
    properties.put(EntityManagerProperties.PERSISTENCE_CONTEXT_COMMIT_WITHOUT_PERSIST_RULES, "true");
    EntityManager em = createEntityManager(properties);
    beginTransaction(em);
    Employee employee = new Employee();
    employee.setLastName("SomeName");
    Address addr = new Address();
    addr.setCity("Douglas");
    try {
        em.persist(employee);
        commitTransaction(em);
        verifyObjectInCacheAndDatabase(employee);
        closeEntityManager(em);
        em = createEntityManager(properties);
        beginTransaction(em);
        employee = em.find(Employee.class, employee.getId());
        employee.setAddress(addr);
        addr.getEmployees().add(employee);
        commitTransaction(em);
        verifyObjectInCacheAndDatabase(employee);
        closeEntityManager(em);
        em = createEntityManager(properties);
        clearCache();
        beginTransaction(em);
        employee = em.find(Employee.class, employee.getId());
        employee.getAddress().setCountry("country");
        employee.getAddress().getEmployees().size();
        employee.setAddress((Address) null);
        em.remove(employee);
        commitTransaction(em);
        em = createEntityManager(properties);
        beginTransaction(em);
        employee = em.find(Employee.class, employee.getId());
        assertNull("Employee Not Deleted", employee);
        commitTransaction(em);
        closeEntityManager(em);
    } catch (RuntimeException exception) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw exception;
    } finally {
        try {
            em = createEntityManager();
            clearCache();
            beginTransaction(em);
            em.remove(em.find(Address.class, addr.getID()));
            commitTransaction(em);
        } catch (RuntimeException ex) {
        // ignore
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 18 with Address

use of org.eclipse.persistence.testing.models.jpa.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();
    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.advanced.Address) Test(org.junit.Test)

Example 19 with Address

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

the class FetchGroupTrackerWeavingTests method verifyCheckFetchedWithFetchGroup_OneToOne.

@Test
public void verifyCheckFetchedWithFetchGroup_OneToOne() {
    Employee emp = new Employee();
    TestFetchGroup fg = new TestFetchGroup();
    ((FetchGroupTracker) emp)._persistence_setFetchGroup(fg);
    assertNull(this.checkAttribute);
    assertNull(this.checkForSetAttribute);
    Address addr = emp.getAddress();
    assertNull(addr);
    assertNull(this.checkForSetAttribute);
    assertNotNull(this.checkAttribute);
    assertEquals("address", this.checkAttribute);
}
Also used : FetchGroupTracker(org.eclipse.persistence.queries.FetchGroupTracker) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) Test(org.junit.Test)

Example 20 with Address

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

the class FetchGroupTrackerWeavingTests method verifyCheckFetchedForSetWithFetchGroup_OneToOne.

@Test
public void verifyCheckFetchedForSetWithFetchGroup_OneToOne() {
    Employee emp = new Employee();
    TestFetchGroup fg = new TestFetchGroup();
    ((FetchGroupTracker) emp)._persistence_setFetchGroup(fg);
    assertNull(this.checkAttribute);
    assertNull(this.checkForSetAttribute);
    emp.setAddress(new Address());
    assertNull(this.checkAttribute);
    assertNotNull(this.checkForSetAttribute);
    assertEquals("address", this.checkForSetAttribute);
}
Also used : FetchGroupTracker(org.eclipse.persistence.queries.FetchGroupTracker) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) Test(org.junit.Test)

Aggregations

Address (org.eclipse.persistence.testing.models.jpa.advanced.Address)79 EntityManager (jakarta.persistence.EntityManager)76 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)53 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)46 PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)13 Query (jakarta.persistence.Query)12 QuerySQLTracker (org.eclipse.persistence.testing.framework.QuerySQLTracker)12 ArrayList (java.util.ArrayList)9 List (java.util.List)9 HashMap (java.util.HashMap)7 TypedQuery (jakarta.persistence.TypedQuery)6 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)6 Department (org.eclipse.persistence.testing.models.jpa.advanced.Department)6 Test (org.junit.Test)6 EntityExistsException (jakarta.persistence.EntityExistsException)5 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)5 Map (java.util.Map)5 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)5 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)5 PersistenceException (jakarta.persistence.PersistenceException)4