Search in sources :

Example 26 with Department

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

the class EntityManagerJUnitTestSuite method testPESSIMISTIC_FORCE_INCREMENTLockOnNonVersionedEntity.

public void testPESSIMISTIC_FORCE_INCREMENTLockOnNonVersionedEntity() {
    Department dept = null;
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        dept = new Department();
        em.persist(dept);
        commitTransaction(em);
    } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw ex;
    }
    try {
        beginTransaction(em);
        dept = em.find(Department.class, dept.getId(), LockModeType.PESSIMISTIC_FORCE_INCREMENT);
        rollbackTransaction(em);
        fail("An Expected jakarta.persistence.PersistenceException was not thrown");
    } catch (PersistenceException ex) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) PersistenceException(jakarta.persistence.PersistenceException)

Example 27 with Department

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

the class AdvancedJPAJunitTest method testMapBuildReferencesPKList.

public void testMapBuildReferencesPKList() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    ClassDescriptor descriptor;
    AbstractSession session = (AbstractSession) JpaHelper.getEntityManager(em).getActiveSession();
    descriptor = session.getDescriptorForAlias("ADV_DEPT");
    Department dept = (Department) em.createQuery("SELECT d FROM ADV_DEPT d WHERE d.equipment IS NOT EMPTY").getResultList().get(0);
    ForeignReferenceMapping mapping = (ForeignReferenceMapping) descriptor.getMappingForAttributeName("equipment");
    Object[] pks = mapping.buildReferencesPKList(dept, mapping.getAttributeValueFromObject(dept), session);
    assertTrue("PK list is of incorrect size.  pks.size: " + pks.length + " expected: " + (dept.getEquipment().size() * 2), pks.length == (dept.getEquipment().size() * 2));
    Map<Integer, Equipment> equipments = (Map<Integer, Equipment>) mapping.valueFromPKList(pks, null, session);
    assertTrue("ValueFromPKList returned list of different size from actual entity.", equipments.size() == dept.getEquipment().size());
    for (Equipment equip : dept.getEquipment().values()) {
        assertTrue("Equipment not found in ValueFromPKList list", equipments.containsKey(equip.getId()));
    }
    rollbackTransaction(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment) Map(java.util.Map) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 28 with Department

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

the class AdvancedJPAJunitTest method testAddNewEquipmentToDepartment.

/**
 * Tests adding objects to a 1-M mapping that uses a map.
 */
public void testAddNewEquipmentToDepartment() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        EJBQueryImpl query = (EJBQueryImpl) em.createNamedQuery("findAllSQLEquipment");
        Collection<Equipment> equipment = query.getResultCollection();
        if (equipment.isEmpty()) {
            fail("No Equipment was found. testCreateNewEquipment should have created new equipment and should have run before this test.");
        } else {
            Department department = new Department();
            department.setName("Department with equipment");
            for (Equipment e : equipment) {
                department.addEquipment(e);
            }
            em.persist(department);
            deptId = department.getId();
            commitTransaction(em);
        }
    } 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) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl)

Example 29 with Department

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

the class NestedFetchGroupTests method dynamicFetchGroup_ElementCollection.

@Test
public void dynamicFetchGroup_ElementCollection() {
    EntityManager em = createEntityManager();
    AttributeGroup compt = new AttributeGroup(null, Competency.class, true);
    compt.addAttribute("description");
    AttributeGroup fg = new AttributeGroup(null, org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department.class, true);
    fg.addAttribute("competencies", compt);
    clearCache();
    Collection<org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department> results = em.createQuery("select d from Department d").setHint(QueryHints.FETCH_GROUP, fg.toFetchGroup()).getResultList();
    for (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department dept : results) {
        assertFalse("Collection fetched: scientists, fg ignored", ((FetchGroupTracker) dept)._persistence_isAttributeFetched("scientists"));
        assertFalse("Collection fetched: offices, fg ignored", ((FetchGroupTracker) dept)._persistence_isAttributeFetched("offices"));
        assertTrue("Collection not fetched: competencies, fg ignored", ((FetchGroupTracker) dept)._persistence_isAttributeFetched("competencies"));
        for (Competency embeded : dept.getCompetencies()) {
            assertTrue("Element attribute not loaded: description, fg ignored", ((FetchGroupTracker) embeded)._persistence_isAttributeFetched("description"));
        }
        dept.getScientists().size();
        assertTrue("Collection not fetched: scientists, fg ignored", ((FetchGroupTracker) dept)._persistence_isAttributeFetched("scientists"));
        assertTrue("Collection not fetched: offices, fg ignored", ((FetchGroupTracker) dept)._persistence_isAttributeFetched("offices"));
        assertTrue("Collection not fetched: competencies, fg ignored", ((FetchGroupTracker) dept)._persistence_isAttributeFetched("competencies"));
        for (Competency embeded : dept.getCompetencies()) {
            embeded.getRating();
            assertTrue("Element attribute not loaded: description, fg ignored", ((FetchGroupTracker) embeded)._persistence_isAttributeFetched("description"));
        }
    }
}
Also used : AttributeGroup(org.eclipse.persistence.queries.AttributeGroup) EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Competency(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Competency) Test(org.junit.Test)

Example 30 with Department

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

the class NestedFetchGroupTests method allNestedFetchGroupWithJoinFetch.

@Test
public void allNestedFetchGroupWithJoinFetch() {
    EntityManager em = createEntityManager();
    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.advanced.Project) EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Query(jakarta.persistence.Query) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) FetchGroup(org.eclipse.persistence.queries.FetchGroup) EntityFetchGroup(org.eclipse.persistence.internal.queries.EntityFetchGroup) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) Test(org.junit.Test)

Aggregations

Department (org.eclipse.persistence.testing.models.jpa.advanced.Department)32 EntityManager (jakarta.persistence.EntityManager)31 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)23 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)10 Equipment (org.eclipse.persistence.testing.models.jpa.advanced.Equipment)10 UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)8 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)6 PersistenceException (jakarta.persistence.PersistenceException)6 Address (org.eclipse.persistence.testing.models.jpa.advanced.Address)6 EntityExistsException (jakarta.persistence.EntityExistsException)5 EntityNotFoundException (jakarta.persistence.EntityNotFoundException)5 NoResultException (jakarta.persistence.NoResultException)5 OptimisticLockException (jakarta.persistence.OptimisticLockException)5 Query (jakarta.persistence.Query)5 RollbackException (jakarta.persistence.RollbackException)5 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)5 SQLException (java.sql.SQLException)5 HashMap (java.util.HashMap)5 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)5 IntegrityException (org.eclipse.persistence.exceptions.IntegrityException)5