Search in sources :

Example 1 with DepartmentAdminRole

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

the class AdvancedCompositePKJunitTest method testDepartmentAdmin.

public void testDepartmentAdmin() {
    String location = "Ottawa";
    String depName = "New Product Research";
    String depRole = "R&D new technologies";
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee emp = new Employee();
        emp.setFirstName("George");
        em.persist(emp);
        Administrator adminEmp = new Administrator();
        adminEmp.setContractCompany("George's consulting");
        adminEmp.setEmployee(emp);
        em.persist(adminEmp);
        Department newProductResearch = new Department();
        newProductResearch.setLocation(location);
        newProductResearch.setName(depName);
        newProductResearch.setRole(depRole);
        em.persist(newProductResearch);
        DepartmentAdminRole depAdmin = new DepartmentAdminRole();
        depAdmin.setAdmin(adminEmp);
        depAdmin.setDepartment(newProductResearch);
        em.persist(depAdmin);
        commitTransaction(em);
        DepartmentAdminRolePK depAdminPk = new DepartmentAdminRolePK(depName, depRole, location, adminEmp.getEmployee().getId());
        DepartmentAdminRole cacheObject = em.find(DepartmentAdminRole.class, depAdminPk);
        assertTrue("Find did not return the DepartmentAdminRole", cacheObject != null);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw e;
    } finally {
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department) DepartmentAdminRolePK(org.eclipse.persistence.testing.models.jpa.advanced.derivedid.DepartmentAdminRolePK) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Administrator(org.eclipse.persistence.testing.models.jpa.advanced.derivedid.Administrator) DepartmentAdminRole(org.eclipse.persistence.testing.models.jpa.advanced.derivedid.DepartmentAdminRole)

Example 2 with DepartmentAdminRole

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

the class AdvancedCompositePKJunitTest method testMapWithDerivedId.

public void testMapWithDerivedId() {
    EntityManagerFactory emf = getEntityManagerFactory();
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee emp = new Employee();
        emp.setFirstName("George");
        em.persist(emp);
        Administrator adminEmp = new Administrator();
        adminEmp.setContractCompany("George's consulting");
        adminEmp.setEmployee(emp);
        em.persist(adminEmp);
        Department support = new Department();
        support.setLocation("Ottawa");
        support.setName("Support");
        support.setRole("Support Customers");
        em.persist(support);
        DepartmentAdminRole depAdmin = new DepartmentAdminRole();
        depAdmin.setAdmin(adminEmp);
        depAdmin.setDepartment(support);
        em.persist(depAdmin);
        AdminPool pool = new AdminPool();
        pool.setId(1);
        pool.addAdmin(depAdmin);
        depAdmin.setPool(pool);
        em.persist(pool);
        em.flush();
        em.clear();
        clearCache();
        pool = em.find(AdminPool.class, pool.getId());
        assertTrue("The AdminPool was not found.", pool != null);
        assertTrue("The map did not contain the correct elements.", pool.getAdmins().get(depAdmin.buildDepartmentAdminRolePK()) != null);
    } catch (Exception e) {
        fail("Exception caught while testing maps with derived ids." + e);
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Administrator(org.eclipse.persistence.testing.models.jpa.advanced.derivedid.Administrator) DepartmentAdminRole(org.eclipse.persistence.testing.models.jpa.advanced.derivedid.DepartmentAdminRole) AdminPool(org.eclipse.persistence.testing.models.jpa.advanced.derivedid.AdminPool) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) PersistenceException(jakarta.persistence.PersistenceException)

Aggregations

EntityManager (jakarta.persistence.EntityManager)2 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)2 Department (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department)2 Administrator (org.eclipse.persistence.testing.models.jpa.advanced.derivedid.Administrator)2 DepartmentAdminRole (org.eclipse.persistence.testing.models.jpa.advanced.derivedid.DepartmentAdminRole)2 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)1 PersistenceException (jakarta.persistence.PersistenceException)1 AdminPool (org.eclipse.persistence.testing.models.jpa.advanced.derivedid.AdminPool)1 DepartmentAdminRolePK (org.eclipse.persistence.testing.models.jpa.advanced.derivedid.DepartmentAdminRolePK)1