Search in sources :

Example 1 with Project

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

the class SQLResultSetMappingTestSuite method testSimpleInheritance.

public void testSimpleInheritance() throws Exception {
    SQLResultSetMapping resultSetMapping = new SQLResultSetMapping("SimpleInheritance");
    EntityResult entityResult = new EntityResult(Project.class);
    entityResult.setDiscriminatorColumn("SMALL_DESCRIM");
    resultSetMapping.addResult(entityResult);
    // Use ANSI outer join sytax so that the query works on most of the databases.
    // SQLCall call = new SQLCall("SELECT t0.PROJ_ID, t0.PROJ_TYPE AS SMALL_DESCRIM,  t0.PROJ_NAME, t0.DESCRIP, t0.LEADER_ID, t0.VERSION, t1.BUDGET FROM CMP3_FA_PROJECT t0, CMP3_FA_LPROJECT t1 WHERE t1.PROJ_ID (+)= t0.PROJ_ID");
    SQLCall call = new SQLCall("SELECT t0.PROJ_ID, t0.PROJ_TYPE AS SMALL_DESCRIM,  t0.PROJ_NAME, t0.DESCRIP, t0.LEADER_ID, t0.VERSION, t1.BUDGET FROM CMP3_FA_PROJECT t0 left outer join CMP3_FA_LPROJECT t1 on t1.PROJ_ID = t0.PROJ_ID");
    ResultSetMappingQuery query = new ResultSetMappingQuery(call);
    query.setSQLResultSetMapping(resultSetMapping);
    List results = (List) getServerSession("fieldaccess").executeQuery(query);
    assertNotNull("No result returned", results);
    assertTrue("Empty list returned", (results.size() != 0));
    for (Iterator iterator = results.iterator(); iterator.hasNext(); ) {
        Object project = iterator.next();
        assertTrue("Failed to return a project", (project instanceof Project));
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject) LargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject) Project(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project) Iterator(java.util.Iterator) List(java.util.List)

Example 2 with Project

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

the class SQLResultSetMappingTestSuite method testRefresh.

public void testRefresh() throws Exception {
    SQLResultSetMapping resultSetMapping = new SQLResultSetMapping("ComplicatedInheritance");
    EntityResult entityResult = new EntityResult(Project.class);
    resultSetMapping.addResult(entityResult);
    entityResult.setDiscriminatorColumn("SMALL_DESCRIM");
    SQLCall call = new SQLCall("SELECT t0.PROJ_ID, t0.PROJ_TYPE AS SMALL_DESCRIM, t0.PROJ_NAME, t0.DESCRIP, t0.LEADER_ID, t0.VERSION, t1.BUDGET FROM CMP3_FA_PROJECT t0, CMP3_FA_PROJECT t2, CMP3_FA_LPROJECT t1 WHERE t1.PROJ_ID = t0.PROJ_ID");
    ResultSetMappingQuery query = new ResultSetMappingQuery(call);
    query.setSQLResultSetMapping(resultSetMapping);
    query.setShouldRefreshIdentityMapResult(true);
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        List results = (List) getServerSession("fieldaccess").executeQuery(query);
        assertNotNull("No result returned", results);
        Project project = (Project) results.get(0);
        project.setDescription("To A new changed description");
        results = (List) getServerSession("fieldaccess").executeQuery(query);
        assertNotNull("No result returned", results);
        assertTrue("Empty list returned", (results.size() != 0));
        assertFalse("Object was not refreshed", project.getDescription().equals("To A new changed description"));
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject) LargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject) Project(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) List(java.util.List)

Example 3 with Project

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

the class NestedFetchGroupTests method allNestedFetchGroupWithJoinFetch.

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

Example 4 with Project

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

the class EntityManagerJUnitTestSuite method testDeleteEmployee.

// Test that deleting an employee works correctly.
public void testDeleteEmployee() {
    // seems to be a Spring bug, removing from Spring run for now.
    if ((getServerPlatform() != null) && getServerPlatform().isSpring()) {
        return;
    }
    if (getDatabaseSession().getPlatform().isSymfoware()) {
        getDatabaseSession().logMessage("Test testDeleteEmployee skipped for this platform, " + "The CascadeOnDelete doesn't work on a relation where CascadeType.Remove or CascadeType.All is specified on Symfoware Platform.");
        return;
    }
    Employee employee = new Employee();
    employee.addPhoneNumber(new PhoneNumber("home", "123", "4567"));
    employee.addPhoneNumber(new PhoneNumber("fax", "456", "4567"));
    employee.addResponsibility("work hard");
    employee.addResponsibility("write code");
    employee.addProject(new Project());
    employee.setWorkWeek(new HashSet<Employee.Weekdays>());
    employee.getWorkWeek().add(Employee.Weekdays.MONDAY);
    employee.getWorkWeek().add(Employee.Weekdays.TUESDAY);
    EntityManager em = createEntityManager();
    QuerySQLTracker counter = null;
    try {
        beginTransaction(em);
        em.persist(employee);
        commitTransaction(em);
        closeEntityManager(em);
        clearCache();
        counter = new QuerySQLTracker(getDatabaseSession());
        em = createEntityManager();
        beginTransaction(em);
        employee = em.find(Employee.class, employee.getId());
        counter.getSqlStatements().clear();
        em.remove(employee);
        if (isWeavingEnabled("fieldaccess") && counter.getSqlStatements().size() > 3) {
            fail("Only 2 delete and 1 select should have occured.");
        }
        commitTransaction(em);
        beginTransaction(em);
        verifyDelete(employee, "fieldaccess");
        commitTransaction(em);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        if (counter != null) {
            counter.remove();
        }
        closeEntityManager(em);
    }
}
Also used : LargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SuperLargeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject) Project(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) PhoneNumber(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker)

Example 5 with Project

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

the class EntityManagerJUnitTestSuite method testDatabaseSyncNewObject.

public void testDatabaseSyncNewObject() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Project project = new LargeProject();
        em.persist(project);
        project.setName("Blah");
        project.setTeamLeader(new Employee());
        project.getTeamLeader().addProject(project);
        em.flush();
    } catch (RuntimeException ex) {
        rollbackTransaction(em);
        if (ex instanceof IllegalStateException)
            return;
    }
    fail("Failed to throw illegal argument when finding unregistered new object cascading on database sync");
}
Also used : LargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SuperLargeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject) Project(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) LargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SuperLargeProject)

Aggregations

Project (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project)16 EntityManager (jakarta.persistence.EntityManager)15 LargeProject (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject)13 SmallProject (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject)13 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)12 Employee (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee)10 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SuperLargeProject)10 ArrayList (java.util.ArrayList)6 List (java.util.List)6 PhoneNumber (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber)5 Address (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address)4 Query (jakarta.persistence.Query)3 HashMap (java.util.HashMap)2 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)2 EntityFetchGroup (org.eclipse.persistence.internal.queries.EntityFetchGroup)2 FetchGroup (org.eclipse.persistence.queries.FetchGroup)2 CopyGroup (org.eclipse.persistence.sessions.CopyGroup)2 Department (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Department)2 Test (org.junit.Test)2 TypedQuery (jakarta.persistence.TypedQuery)1