Search in sources :

Example 6 with SmallProject

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

the class EntityManagerJUnitTestSuite method testNestedBatchQueryHints.

public void testNestedBatchQueryHints() {
    String errorMsg = "";
    clearCache();
    ServerSession session = getServerSession();
    QuerySQLTracker counter = new QuerySQLTracker(session);
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        // setup
        int nEmployees = 2;
        int nProjects = 2;
        int nTeamMembers = 2;
        int nDealers = 2;
        int nPhones = 2;
        boolean useLargeProject = false;
        List<Employee> employees = new ArrayList(nEmployees);
        List<Employee> teamMembers = new ArrayList(nEmployees * nProjects * nTeamMembers);
        for (int i = 0; i < nEmployees; i++) {
            String iStr = Integer.toString(i);
            Employee emp = new Employee("Employee_NestedBatchQueryHint", iStr);
            employees.add(emp);
            Employee manager = new Employee("Manager_NestedBatchQueryHint", iStr);
            manager.addManagedEmployee(emp);
            for (int j = 0; j < nProjects; j++) {
                String jStr = Integer.toString(j);
                Project project;
                if (useLargeProject) {
                    project = new LargeProject();
                } else {
                    project = new SmallProject();
                }
                useLargeProject = !useLargeProject;
                project.setName(iStr + jStr);
                manager.addProject(project);
                project.addTeamMember(manager);
                for (int k = 0; k < nTeamMembers; k++) {
                    String kStr = Integer.toString(k);
                    String teamMemberLastName = iStr + jStr + kStr;
                    Employee teamMember = new Employee("TeamMember_NestedBatchQueryHint", teamMemberLastName);
                    teamMembers.add(teamMember);
                    teamMember.addProject(project);
                    project.addTeamMember(teamMember);
                    for (int l = 0; l < nDealers; l++) {
                        String lStr = Integer.toString(l);
                        Dealer dealer = new Dealer(lStr, teamMemberLastName);
                        teamMember.addDealer(dealer);
                    }
                    for (int l = 0; l < nPhones; l++) {
                        String lStr = Integer.toString(l);
                        PhoneNumber phone = new PhoneNumber(lStr, teamMemberLastName, lStr + lStr + lStr + lStr + lStr + lStr + lStr);
                        teamMember.addPhoneNumber(phone);
                    }
                }
            }
        }
        // Persist cacscaded manager, then projects
        for (Employee emp : employees) {
            em.persist(emp);
        }
        // Persist cacscaded dealers and phones
        for (Employee emp : teamMembers) {
            em.persist(emp);
        }
        em.flush();
        // test
        em.clear();
        Query query1 = em.createQuery("SELECT e FROM Employee e WHERE e.firstName = 'Employee_NestedBatchQueryHint' ORDER BY e.lastName");
        query1.setHint(QueryHints.BATCH, "e.manager");
        query1.setHint(QueryHints.BATCH, "e.manager.projects");
        query1.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers");
        query1.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers.dealers");
        query1.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers.phoneNumbers");
        errorMsg += verifyBatchReading("query1", query1, counter, employees, session);
        em.clear();
        Query query2 = em.createQuery("SELECT e FROM Employee e WHERE e.firstName = 'Employee_NestedBatchQueryHint' ORDER BY e.lastName");
        query2.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers");
        query2.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers.dealers");
        query2.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers.phoneNumbers");
        errorMsg += verifyBatchReading("query2", query2, counter, employees, session);
        em.clear();
        Query query3 = em.createQuery("SELECT e FROM Employee e WHERE e.firstName = 'Employee_NestedBatchQueryHint' ORDER BY e.lastName");
        query3.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers.dealers");
        query3.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers.phoneNumbers");
        errorMsg += verifyBatchReading("query3", query3, counter, employees, session);
    } finally {
        // clean-up
        rollbackTransaction(em);
        closeEntityManager(em);
        counter.remove();
    }
    if (errorMsg.length() > 0) {
        fail(errorMsg);
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) 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) ArrayList(java.util.ArrayList) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) HugeProject(org.eclipse.persistence.testing.models.jpa.advanced.HugeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) SpecialHugeProject(org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) Dealer(org.eclipse.persistence.testing.models.jpa.advanced.Dealer)

Example 7 with SmallProject

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

the class EntityManagerJUnitTestSuite method internalTestUpdateAllProjects.

protected void internalTestUpdateAllProjects(Class<?> cls) {
    if ((JUnitTestCase.getServerSession()).getPlatform().isSymfoware()) {
        getServerSession().logMessage("Test testUpdateAll*Projects skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    }
    String className = Helper.getShortClassName(cls);
    String name = "testUpdateAllProjects";
    String newName = "testUpdateAllProjectsNEW";
    HashMap map = null;
    boolean ok = false;
    try {
        // setup
        // save the original names of projects: will set them back in cleanup
        // to restore the original state.
        EntityManager em = createEntityManager();
        List projects = em.createQuery("SELECT OBJECT(p) FROM Project p").getResultList();
        map = new HashMap(projects.size());
        for (int i = 0; i < projects.size(); i++) {
            Project p = (Project) projects.get(i);
            map.put(p.getId(), p.getName());
        }
        // populate Projects - necessary only if no SmallProject and/or LargeProject objects already exist.
        beginTransaction(em);
        try {
            createProjectsWithName(name, null, em);
            commitTransaction(em);
        } finally {
            closeEntityManagerAndTransaction(em);
        }
        // test
        em = createEntityManager();
        beginTransaction(em);
        try {
            em.createQuery("UPDATE " + className + " p set p.name = '" + newName + "'").executeUpdate();
            commitTransaction(em);
        } finally {
            closeEntityManagerAndTransaction(em);
        }
        // verify
        em = createEntityManager();
        String errorMsg = "";
        try {
            projects = em.createQuery("SELECT OBJECT(p) FROM Project p").getResultList();
            for (int i = 0; i < projects.size(); i++) {
                Project p = (Project) projects.get(i);
                String readName = p.getName();
                if (cls.isInstance(p)) {
                    if (!newName.equals(readName)) {
                        errorMsg = errorMsg + "haven't updated name: " + p + "; ";
                    }
                } else {
                    if (newName.equals(readName)) {
                        errorMsg = errorMsg + "have updated name: " + p + "; ";
                    }
                }
            }
        } 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);
        }
        if (errorMsg.length() > 0) {
            fail(errorMsg);
        } else {
            ok = true;
        }
    } finally {
        // clean-up
        try {
            if (map != null) {
                EntityManager em = createEntityManager();
                beginTransaction(em);
                Query query = em.createQuery("SELECT OBJECT(p) FROM Project p");
                if (usesSOP() && !isSOPRecoverable()) {
                    // otherwise will fail because SOP field is set to null after bulk update
                    query.setHint(QueryHints.SERIALIZED_OBJECT, "false");
                }
                List projects = query.getResultList();
                try {
                    for (int i = 0; i < projects.size(); i++) {
                        Project p = (Project) projects.get(i);
                        String oldName = (String) map.get(((Project) projects.get(i)).getId());
                        p.setName(oldName);
                    }
                    commitTransaction(em);
                } finally {
                    closeEntityManagerAndTransaction(em);
                }
            }
            // delete projects that createProjectsWithName has created in setup.
            // these will be left over under the newName
            deleteProjectsWithName(newName);
        } catch (RuntimeException ex) {
            // eat clean-up exception in case the test failed
            if (ok) {
                throw ex;
            }
        }
    }
}
Also used : Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) HugeProject(org.eclipse.persistence.testing.models.jpa.advanced.HugeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) SpecialHugeProject(org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) QueryException(org.eclipse.persistence.exceptions.QueryException) 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) HashMap(java.util.HashMap) PersistenceException(jakarta.persistence.PersistenceException) IndirectList(org.eclipse.persistence.indirection.IndirectList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with SmallProject

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

the class CriteriaQueryCastTestSuite method testDowncastInSelect.

public void testDowncastInSelect() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        LargeProject proj = new LargeProject();
        proj.setBudget(1000);
        proj.setName("test1");
        em.persist(proj);
        SmallProject sp = new SmallProject();
        sp.setName("sp1");
        em.persist(sp);
        em.flush();
        clearCache();
        em.clear();
        // this would work in the past if TYPE was added to the where clause
        // Query query = em.createQuery("Select treat(c as LargeProject).budget from Project c");
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Project> cq = qb.createQuery(Project.class);
        Root<Project> root = cq.from(Project.class);
        Root largeProjectRoot = qb.treat(root, LargeProject.class);
        cq.select(largeProjectRoot.get("budget"));
        List resultList = em.createQuery(cq).getResultList();
        assertTrue("Incorrect results returned", resultList.size() == 1);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) EntityManager(jakarta.persistence.EntityManager) Root(jakarta.persistence.criteria.Root) List(java.util.List) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 9 with SmallProject

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

the class CriteriaQueryCastTestSuite method testDowncastManyToManyQueryKey.

public void testDowncastManyToManyQueryKey() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        LargeProject proj = new LargeProject();
        proj.setBudget(1000);
        proj.setName("test1");
        em.persist(proj);
        SmallProject sp = new SmallProject();
        sp.setName("sp1");
        em.persist(sp);
        Employee emp = new Employee();
        emp.setFirstName("Reggie");
        emp.setLastName("Josephson");
        emp.addProject(proj);
        proj.addTeamMember(emp);
        emp.addProject(sp);
        sp.addTeamMember(emp);
        em.persist(emp);
        emp = new Employee();
        emp.setFirstName("Ron");
        emp.setLastName("Josephson");
        emp.addProject(sp);
        sp.addTeamMember(emp);
        em.persist(emp);
        em.flush();
        clearCache();
        em.clear();
        // Query query = em.createQuery("Select e from Employee e join treat(e.projects as LargeProject) p where p.budget > 100");
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        Root<Employee> root = cq.from(Employee.class);
        Join projectsJoin = root.join("projects");
        Join largeProjectJoin = qb.treat(projectsJoin, LargeProject.class);
        cq.where(qb.gt(largeProjectJoin.get("budget"), 100));
        List resultList = em.createQuery(cq).getResultList();
        assertTrue("Incorrect results returned", resultList.size() == 1);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Join(jakarta.persistence.criteria.Join) List(java.util.List) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 10 with SmallProject

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

the class CriteriaQueryCastTestSuite method testDowncastManyToManyExpressionBuilder.

public void testDowncastManyToManyExpressionBuilder() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        LargeProject proj = new LargeProject();
        proj.setBudget(1000);
        proj.setName("test1");
        em.persist(proj);
        SmallProject sp = new SmallProject();
        sp.setName("sp1");
        em.persist(sp);
        Employee emp = new Employee();
        emp.setFirstName("Reggie");
        emp.setLastName("Josephson");
        emp.addProject(proj);
        proj.addTeamMember(emp);
        emp.addProject(sp);
        sp.addTeamMember(emp);
        em.persist(emp);
        emp = new Employee();
        emp.setFirstName("Ron");
        emp.setLastName("Josephson");
        emp.addProject(sp);
        sp.addTeamMember(emp);
        em.persist(emp);
        em.flush();
        clearCache();
        em.clear();
        // JpaQuery query = (JpaQuery)em.createQuery("Select p from Project p where treat (p as LargeProject).budget > 100");
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Project> cq = qb.createQuery(Project.class);
        Root<Project> root = cq.from(Project.class);
        Root largeProjectRoot = qb.treat(root, LargeProject.class);
        cq.where(qb.gt(largeProjectRoot.get("budget"), 100));
        List resultList = em.createQuery(cq).getResultList();
        assertTrue("Incorrect results returned", resultList.size() == 1);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Root(jakarta.persistence.criteria.Root) List(java.util.List) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Aggregations

SmallProject (org.eclipse.persistence.testing.models.jpa.advanced.SmallProject)30 EntityManager (jakarta.persistence.EntityManager)27 LargeProject (org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)21 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)19 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)17 List (java.util.List)13 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)11 Project (org.eclipse.persistence.testing.models.jpa.advanced.Project)11 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)9 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject)9 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)8 HugeProject (org.eclipse.persistence.testing.models.jpa.advanced.HugeProject)8 SpecialHugeProject (org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject)8 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)7 Query (jakarta.persistence.Query)6 ArrayList (java.util.ArrayList)6 ReportQuery (org.eclipse.persistence.queries.ReportQuery)6 Vector (java.util.Vector)5 Expression (org.eclipse.persistence.expressions.Expression)5 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)5