Search in sources :

Example 26 with Address

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

the class EntityManagerJUnitTestSuite method testManyToOnePersistCascadeOnFlush.

// this test was failing after transaction ailitche_main_6333458_070821
public void testManyToOnePersistCascadeOnFlush() {
    boolean pass = false;
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        String firstName = "testManyToOneContains";
        Address address = new Address();
        address.setCountry(firstName);
        Employee employee = new Employee();
        employee.setFirstName(firstName);
        em.persist(employee);
        employee.setAddress(address);
        em.flush();
        pass = em.contains(address);
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
    if (!pass) {
        fail("em.contains(address) returned false");
    }
}
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 27 with Address

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

the class EntityManagerJUnitTestSuite method testIsLoadedWithoutReference.

public void testIsLoadedWithoutReference() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee emp = new Employee();
        emp.setFirstName("Abe");
        emp.setLastName("Jones");
        Address addr = new Address();
        addr.setCity("Palo Alto");
        emp.setAddress(addr);
        PhoneNumber pn = new PhoneNumber();
        pn.setNumber("1234456");
        pn.setType("Home");
        emp.addPhoneNumber(pn);
        pn.setOwner(emp);
        Employee manager = new Employee();
        manager.addManagedEmployee(emp);
        emp.setManager(manager);
        em.persist(emp);
        em.flush();
        em.clear();
        clearCache();
        ProviderUtil util = (new PersistenceProvider()).getProviderUtil();
        if (emp instanceof PersistenceWeaved) {
            assertTrue("ProviderUtil did not return LOADED for isLoaded when it should.", util.isLoaded(emp).equals(LoadState.LOADED));
            emp = em.getReference(Employee.class, emp.getId());
            // If fetch group weaving is off then we will load EAGER mappings and LOADED will be returned.
            if (isWeavingForFetchGroupsEnabled()) {
                assertTrue("ProviderUtil did not return NOT_LOADED for isLoaded when it should.", util.isLoaded(emp).equals(LoadState.NOT_LOADED));
            } else {
                assertTrue("ProviderUtil did not return LOADED for isLoaded when it should.", util.isLoaded(emp).equals(LoadState.LOADED));
            }
        } else {
            assertTrue("(NonWeaved) ProviderUtil did not return UNKNOWN for isLoaded when it should.", util.isLoaded(emp).equals(LoadState.UNKNOWN));
            emp = em.getReference(Employee.class, emp.getId());
            assertTrue("(NonWeaved)  ProviderUtil did not return UNKNOWN for isLoaded when it should.", util.isLoaded(emp).equals(LoadState.UNKNOWN));
        }
        assertTrue("ProviderUtil did not return UNKNOWN for isLoaded when it should.", util.isLoaded(new NonEntity()).equals(LoadState.UNKNOWN));
    } finally {
        rollbackTransaction(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ProviderUtil(jakarta.persistence.spi.ProviderUtil) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) PersistenceWeaved(org.eclipse.persistence.internal.weaving.PersistenceWeaved) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) NonEntity(org.eclipse.persistence.testing.models.jpa.advanced.NonEntity)

Example 28 with Address

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

the class EntityManagerJUnitTestSuite method internalUpdateUsingTempStorage.

protected void internalUpdateUsingTempStorage(boolean useParameter) {
    if ((JUnitTestCase.getServerSession()).getPlatform().isSymfoware()) {
        getServerSession().logMessage("Test testUpdateUsingTempStorage* skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    } else if ((JUnitTestCase.getServerSession()).getPlatform().isPervasive()) {
        getServerSession().logMessage("Test testUpdateUsingTempStorage* skipped for this platform. " + "Pervasive does not support dynamic parameters in the Select list.");
        return;
    }
    String firstName = "testUpdateUsingTempStorage";
    int n = 3;
    // setup
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        // make sure there are no pre-existing objects with this name
        em.createQuery("DELETE FROM Employee e WHERE e.firstName = '" + firstName + "'").executeUpdate();
        em.createQuery("DELETE FROM Address a WHERE a.country = '" + firstName + "'").executeUpdate();
        // populate Employees
        for (int i = 1; i <= n; i++) {
            Employee emp = new Employee();
            emp.setFirstName(firstName);
            emp.setLastName(Integer.toString(i));
            emp.setSalary(i * 100);
            emp.setRoomNumber(i);
            Address address = new Address();
            address.setCountry(firstName);
            address.setCity(Integer.toString(i));
            emp.setAddress(address);
            em.persist(emp);
        }
        commitTransaction(em);
    } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw ex;
    } finally {
        closeEntityManager(em);
    }
    // test
    em = createEntityManager();
    beginTransaction(em);
    int nUpdated = 0;
    try {
        if (useParameter) {
            nUpdated = em.createQuery("UPDATE Employee e set e.salary = e.roomNumber, e.roomNumber = e.salary, e.address = :address where e.firstName = '" + firstName + "'").setParameter("address", null).executeUpdate();
        } else {
            nUpdated = em.createQuery("UPDATE Employee e set e.salary = e.roomNumber, e.roomNumber = e.salary, e.address = null where e.firstName = '" + firstName + "'").executeUpdate();
        }
        commitTransaction(em);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
    // verify
    String error = null;
    em = createEntityManager();
    try {
        List result = em.createQuery("SELECT OBJECT(e) FROM Employee e WHERE e.firstName = '" + firstName + "'").getResultList();
        int nReadBack = result.size();
        if (n != nUpdated) {
            error = "n = " + n + ", but nUpdated =" + nUpdated + ";";
        }
        if (n != nReadBack) {
            error = " n = " + n + ", but nReadBack =" + nReadBack + ";";
        }
        for (int i = 0; i < nReadBack; i++) {
            Employee emp = (Employee) result.get(i);
            if (emp.getAddress() != null) {
                error = " Employee " + emp.getLastName() + " still has address;";
            }
            int ind = Integer.parseInt(emp.getLastName());
            if (emp.getSalary() != ind) {
                error = " Employee " + emp.getLastName() + " has wrong salary " + emp.getSalary() + ";";
            }
            if (emp.getRoomNumber() != ind * 100) {
                error = " Employee " + emp.getLastName() + " has wrong roomNumber " + emp.getRoomNumber() + ";";
            }
        }
    } catch (RuntimeException ex) {
        if (usesSOP() && !isSOPRecoverable()) {
            if (ex instanceof PersistenceException) {
                if (ex.getCause() instanceof QueryException && ((QueryException) ex.getCause()).getErrorCode() == QueryException.SOP_OBJECT_IS_NOT_FOUND) {
                // getResultList is expected to fail because SOP field is set to null after bulk update
                } else {
                    fail("Wrong cause of PersistenceException: " + ex.getCause());
                }
            } else {
                fail("PersistenceException was expected");
            }
        } else {
            throw ex;
        }
    } finally {
        closeEntityManager(em);
    }
    // clean up
    em = createEntityManager();
    try {
        beginTransaction(em);
        // make sure there are no objects left with this name
        em.createQuery("DELETE FROM Employee e WHERE e.firstName = '" + firstName + "'").executeUpdate();
        em.createQuery("DELETE FROM Address a WHERE a.country = '" + firstName + "'").executeUpdate();
        commitTransaction(em);
    } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw ex;
    } finally {
        closeEntityManager(em);
    }
    if (error != null) {
        fail(error);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) QueryException(org.eclipse.persistence.exceptions.QueryException) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) PersistenceException(jakarta.persistence.PersistenceException) IndirectList(org.eclipse.persistence.indirection.IndirectList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 29 with Address

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

the class EntityManagerJUnitTestSuite method testInitializeFieldForPropertyAccess.

/**
 * 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 before the object is managed
 */
public void testInitializeFieldForPropertyAccess() {
    Employee employee = new Employee();
    employee.setFirstName("Andy");
    employee.setLastName("Dufresne");
    Address address = new Address();
    address.setCity("Shawshank");
    employee.setAddressField(address);
    EntityManager em = createEntityManager();
    beginTransaction(em);
    em.persist(employee);
    try {
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw e;
    }
    int id = employee.getId();
    clearCache();
    em = createEntityManager();
    beginTransaction(em);
    try {
        employee = em.find(Employee.class, id);
        address = employee.getAddress();
        assertTrue("The address was not persisted.", employee.getAddress() != null);
        assertTrue("The address was not correctly persisted.", employee.getAddress().getCity().equals("Shawshank"));
    } finally {
        employee.setAddress((Address) null);
        em.remove(address);
        em.remove(employee);
        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 30 with Address

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

the class AdvancedJPAJunitTest method testNamedStoredProcedureQuery.

/**
 * Tests a @NamedStoredProcedureQuery.
 */
public void testNamedStoredProcedureQuery() {
    if (!supportsStoredProcedures()) {
        return;
    }
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Address address1 = new Address();
        address1.setCity("Ottawa");
        address1.setPostalCode("K1G6P3");
        address1.setProvince("ON");
        address1.setStreet("123 Street");
        address1.setCountry("Canada");
        em.persist(address1);
        commitTransaction(em);
        // 260263 and 302316: clear the cache or we will end up with a false positive when comparing the entity to itself later
        em.clear();
        Address address2 = (Address) em.createNamedQuery("SProcAddress").setParameter("ADDRESS_ID", address1.getID()).getSingleResult();
        assertNotNull("Address returned from stored procedure is null", address2);
        // new
        assertFalse("Address returned is the same cached instance that was persisted - the cache must be disabled for this test", address1 == address2);
        assertTrue("Address build correctly using stored procedure", (address2.getID() == address1.getID()));
        assertTrue("Address build correctly using stored procedure", (address2.getStreet().equals(address1.getStreet())));
        assertTrue("Address build correctly using stored procedure", (address2.getCountry().equals(address1.getCountry())));
        assertTrue("Address build correctly using stored procedure", (address2.getProvince().equals(address1.getProvince())));
    } 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) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address)

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