Search in sources :

Example 11 with Address

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

the class EntityManagerJUnitTestSuite method testIgnoreRemovedObjectsOnDatabaseSync.

public void testIgnoreRemovedObjectsOnDatabaseSync() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    Query phoneQuery = em.createQuery("Select p from PhoneNumber p where p.owner.lastName like 'Dow%'");
    Query empQuery = em.createQuery("Select e FROM Employee e where e.lastName like 'Dow%'");
    // --setup
    try {
        Employee emp = new Employee();
        emp.setLastName("Dowder");
        PhoneNumber phone = new PhoneNumber("work", "613", "5555555");
        emp.addPhoneNumber(phone);
        phone = new PhoneNumber("home", "613", "4444444");
        emp.addPhoneNumber(phone);
        Address address = new Address("SomeStreet", "somecity", "province", "country", "postalcode");
        emp.setAddress(address);
        em.persist(emp);
        em.flush();
        emp = new Employee();
        emp.setLastName("Dows");
        phone = new PhoneNumber("work", "613", "2222222");
        emp.addPhoneNumber(phone);
        phone = new PhoneNumber("home", "613", "1111111");
        emp.addPhoneNumber(phone);
        address = new Address("street1", "city1", "province1", "country1", "postalcode1");
        emp.setAddress(address);
        em.persist(emp);
        em.flush();
        // --end setup
        List<Employee> emps = empQuery.getResultList();
        List phones = phoneQuery.getResultList();
        for (Iterator iterator = phones.iterator(); iterator.hasNext(); ) {
            em.remove(iterator.next());
        }
        em.flush();
        for (Iterator<Employee> iterator = emps.iterator(); iterator.hasNext(); ) {
            em.remove(iterator.next());
        }
    } catch (RuntimeException ex) {
        rollbackTransaction(em);
        throw ex;
    }
    try {
        em.flush();
    } catch (IllegalStateException ex) {
        rollbackTransaction(em);
        closeEntityManager(em);
        em = createEntityManager();
        beginTransaction(em);
        try {
            phoneQuery = em.createQuery("Select p from PhoneNumber p where p.owner.lastName like 'Dow%'");
            empQuery = em.createQuery("Select e from Employee e where e.lastName like 'Dow%'");
            List<Employee> emps = empQuery.getResultList();
            List phones = phoneQuery.getResultList();
            for (Iterator iterator = phones.iterator(); iterator.hasNext(); ) {
                em.remove(iterator.next());
            }
            for (Iterator<Employee> iterator = emps.iterator(); iterator.hasNext(); ) {
                em.remove(iterator.next());
            }
            commitTransaction(em);
        } catch (RuntimeException re) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            throw re;
        }
        fail("Failed to ignore the removedobject when cascading on database sync");
    }
    commitTransaction(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) ReportQuery(org.eclipse.persistence.queries.ReportQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Query(jakarta.persistence.Query) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ValueReadQuery(org.eclipse.persistence.queries.ValueReadQuery) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery) TypedQuery(jakarta.persistence.TypedQuery) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) Iterator(java.util.Iterator) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) IndirectList(org.eclipse.persistence.indirection.IndirectList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 12 with Address

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

the class EntityManagerJUnitTestSuite method testMultipleExistenceChecksForDetachedObjects.

public void testMultipleExistenceChecksForDetachedObjects() {
    EntityManager em = createEntityManager();
    Department dept1 = new Department("Dept1");
    Department dept2 = new Department("Dept2");
    Department dept3 = new Department("DetachedDept");
    Employee emp = new Employee("Bob", "Bob");
    try {
        List<Address> results = em.createQuery("Select a from Address a").getResultList();
        beginTransaction(em);
        em.persist(emp);
        em.persist(dept1);
        em.persist(dept2);
        commitTransaction(em);
        em.clear();
        beginTransaction(em);
        emp = em.find(Employee.class, emp.getId());
        emp.setDepartment(dept2);
        em.flush();
        em.persist(dept3);
        emp.setDepartment(dept3);
        em.flush();
        emp.setDepartment(dept1);
        em.flush();
        commitTransaction(em);
        closeEntityManager(em);
        verifyObjectInCacheAndDatabase(emp);
    } finally {
        try {
            em = createEntityManager();
            beginTransaction(em);
            em.remove(em.find(Department.class, dept1.getId()));
            em.remove(em.find(Department.class, dept2.getId()));
            em.remove(em.find(Department.class, dept3.getId()));
            em.remove(em.find(Employee.class, emp.getId()));
            commitTransaction(em);
        } catch (Exception e) {
        // ignore
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) TestProblemException(org.eclipse.persistence.testing.framework.TestProblemException) ValidationException(org.eclipse.persistence.exceptions.ValidationException) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) EntityExistsException(jakarta.persistence.EntityExistsException) OptimisticLockException(jakarta.persistence.OptimisticLockException) QueryException(org.eclipse.persistence.exceptions.QueryException) TransactionRequiredException(jakarta.persistence.TransactionRequiredException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) EntityNotFoundException(jakarta.persistence.EntityNotFoundException) SQLException(java.sql.SQLException) RollbackException(jakarta.persistence.RollbackException) NoResultException(jakarta.persistence.NoResultException) PersistenceException(jakarta.persistence.PersistenceException) PersistenceUnitLoadingException(org.eclipse.persistence.exceptions.PersistenceUnitLoadingException)

Example 13 with Address

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

the class EntityManagerJUnitTestSuite method testMergeNewObject.

// bug6167431: tests calling merge on a new object puts it in the cache, and that all new objects in the tree get IDs generated
public void testMergeNewObject() {
    // create an Employee
    Employee emp = new Employee();
    emp.setFirstName("testMergeNewObjectEmployee");
    emp.setAddress(new Address("45 O'Connor", "Ottawa", "Ont", "Canada", "K1P1A4"));
    // persist the Employee
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        Employee managedEmp = em.merge(emp);
        assertNotNull("merged Employee doesn't have its ID generated", managedEmp.getId());
        assertNotNull("merged Employee cannot be found using find", em.find(Employee.class, managedEmp.getId()));
    // this won't work till bug:6193761 is fixed
    // this.assertTrue("referenced Address doesn't have its ID generated", managedEmp.getAddress().getId()!=0);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(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 14 with Address

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

the class EntityManagerJUnitTestSuite method testRefreshInvalidateDeletedObject.

// Bug5955326, refresh should invalidate the shared cached object that was deleted outside of JPA.
public void testRefreshInvalidateDeletedObject() {
    EntityManager em1 = createEntityManager();
    EntityManager em2 = createEntityManager();
    Address address = new Address();
    address.setCity("Kanata");
    // persist the Address
    try {
        // Ensure shared cache being used.
        boolean isIsolated = ((EntityManagerImpl) em1).getServerSession().getClassDescriptorForAlias("Address").isIsolated();
        if (isIsolated) {
            throw new Exception("This test should use non-isolated cache setting class descriptor for test.");
        }
        beginTransaction(em1);
        em1.persist(address);
        commitTransaction(em1);
        // Cache the Address
        em1 = createEntityManager();
        beginTransaction(em1);
        address = em1.find(Address.class, address.getID());
        // Delete Address outside of JPA so that the object still stored in the cache.
        em2 = createEntityManager();
        beginTransaction(em2);
        em2.createNativeQuery("DELETE FROM CMP3_ADDRESS where ADDRESS_ID = ?1").setParameter(1, address.getID()).executeUpdate();
        commitTransaction(em2);
        // Call refresh to invalidate the object
        em1.refresh(address);
    } catch (Exception e) {
    // expected exception
    } finally {
        if (isTransactionActive(em1)) {
            rollbackTransaction(em1);
        }
    }
    // Verify
    beginTransaction(em1);
    address = em1.find(Address.class, address.getID());
    commitTransaction(em1);
    assertNull("The deleted object is still valid in share cache", address);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) TestProblemException(org.eclipse.persistence.testing.framework.TestProblemException) ValidationException(org.eclipse.persistence.exceptions.ValidationException) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) EntityExistsException(jakarta.persistence.EntityExistsException) OptimisticLockException(jakarta.persistence.OptimisticLockException) QueryException(org.eclipse.persistence.exceptions.QueryException) TransactionRequiredException(jakarta.persistence.TransactionRequiredException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) EntityNotFoundException(jakarta.persistence.EntityNotFoundException) SQLException(java.sql.SQLException) RollbackException(jakarta.persistence.RollbackException) NoResultException(jakarta.persistence.NoResultException) PersistenceException(jakarta.persistence.PersistenceException) PersistenceUnitLoadingException(org.eclipse.persistence.exceptions.PersistenceUnitLoadingException)

Example 15 with Address

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

the class EntityManagerJUnitTestSuite method testNoPersistOnCommit.

// Test Not using the persist operation on commit.
public void testNoPersistOnCommit() {
    // Properties only works in jse.
    if (isOnServer()) {
        return;
    }
    EntityManager em = createEntityManager();
    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();
        beginTransaction(em);
        ((RepeatableWriteUnitOfWork) JpaHelper.getEntityManager(em).getUnitOfWork()).setDiscoverUnregisteredNewObjectsWithoutPersist(true);
        employee = em.find(Employee.class, employee.getId());
        employee.setAddress(addr);
        addr.getEmployees().add(employee);
        commitTransaction(em);
        verifyObjectInCacheAndDatabase(employee);
        closeEntityManager(em);
        em = createEntityManager();
        clearCache();
        beginTransaction(em);
        ((RepeatableWriteUnitOfWork) JpaHelper.getEntityManager(em).getUnitOfWork()).setDiscoverUnregisteredNewObjectsWithoutPersist(true);
        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();
        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) RepeatableWriteUnitOfWork(org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork)

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