Search in sources :

Example 61 with Project

use of org.eclipse.persistence.testing.models.jpa.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_PROJECT t0, CMP3_PROJECT t2, CMP3_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().executeQuery(query);
        assertNotNull("No result returned", results);
        Project project = (Project) results.get(0);
        project.setDescription("To A new changed description");
        results = (List) getServerSession().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.advanced.SmallProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) List(java.util.List)

Example 62 with Project

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

the class AdvancedJPAJunitTest method testEmployeeToProjectWithBatchFetchTypeWithSmallFetchSize.

/**
 * Bug 1148 (GitHub)
 * Test batch fetch with small fetch size
 */
public void testEmployeeToProjectWithBatchFetchTypeWithSmallFetchSize() {
    final String lastName = "testRandomEmployeeToProject";
    // Set up
    Set<Employee> employeesToRemove = new HashSet<>();
    Set<Integer> employeeIds = new HashSet<>();
    EntityManager em = createEntityManager();
    for (int i = 0; i < 100; i++) {
        beginTransaction(em);
        Employee employee = new Employee();
        employee.setLastName(lastName);
        employeesToRemove.add(employee);
        em.persist(employee);
        employeeIds.add(employee.getId());
        for (int j = 0; j < 20; j++) {
            Project project = new Project();
            employee.addProject(project);
            em.persist(project);
        }
        commitTransaction(em);
    }
    JpaEntityManager jpaEntityManager = (JpaEntityManager) em.getDelegate();
    jpaEntityManager.getUnitOfWork().getIdentityMapAccessor().initializeAllIdentityMaps();
    try {
        String jpql = "SELECT employee FROM Employee employee WHERE employee.id IN :ids";
        TypedQuery<Employee> query = em.createQuery(jpql, Employee.class).setParameter("ids", employeeIds).setHint(QueryHints.BATCH_TYPE, BatchFetchType.IN).setHint(QueryHints.BATCH, "employee.projects.properties").setHint(QueryHints.BATCH_SIZE, 3);
        List<Employee> employees = query.getResultList();
        // Trigger the bug
        Collections.shuffle(employees);
        int count = 0;
        try {
            for (Employee employee : employees) {
                for (Project project : employee.getProjects()) {
                    count++;
                }
            }
            Assert.assertEquals("Project objects received are not as many as expected", 2000, count);
        } catch (ArrayIndexOutOfBoundsException x) {
            Assert.fail(Helper.printStackTraceToString(x));
        }
    } finally {
        // Clean up
        beginTransaction(em);
        for (Employee employee : employeesToRemove) {
            employee = em.merge(employee);
            for (Project project : employee.getProjects()) {
                em.remove(em.merge(project));
            }
            em.remove(employee);
        }
        commitTransaction(em);
        closeEntityManager(em);
    }
}
Also used : JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) 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) 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) HashSet(java.util.HashSet)

Example 63 with Project

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

the class AdvancedJPAJunitTest method testEmployeeToProjectWithBatchFetchTypeInCustomIteration.

/**
 * Bug 412056
 * Test batch fetch with size smaller than results with custom iteration
 */
public void testEmployeeToProjectWithBatchFetchTypeInCustomIteration() {
    final String lastName = "testEmployeeToProject";
    // Set up
    Set<Employee> employeesToRemove = new HashSet<>();
    EntityManager em = createEntityManager();
    for (int i = 0; i < 100; i++) {
        beginTransaction(em);
        Employee employee = new Employee();
        employee.setLastName(lastName);
        employeesToRemove.add(employee);
        em.persist(employee);
        for (int j = 0; j < 20; j++) {
            Project project = new Project();
            employee.addProject(project);
            em.persist(project);
        }
        commitTransaction(em);
    }
    JpaEntityManager jpaEntityManager = (JpaEntityManager) em.getDelegate();
    jpaEntityManager.getUnitOfWork().getIdentityMapAccessor().initializeAllIdentityMaps();
    try {
        Expression exp = new ExpressionBuilder(Employee.class);
        EJBQueryImpl query = (EJBQueryImpl) jpaEntityManager.createQuery(exp, Employee.class);
        ((ReadAllQuery) query.getDatabaseQuery()).addBatchReadAttribute("projects");
        ((ReadAllQuery) query.getDatabaseQuery()).setBatchFetchType(BatchFetchType.IN);
        ((ReadAllQuery) query.getDatabaseQuery()).setBatchFetchSize(10);
        List<Employee> employees = query.getResultList();
        // Trigger the bug
        employees.add(employees.remove(0));
        int count = 0;
        try {
            for (Employee employee : employees) {
                if (lastName.equals(employee.getLastName())) {
                    for (Project project : employee.getProjects()) {
                        count++;
                    }
                }
            }
            Assert.assertEquals("Project objects received are not as many as expected", 2000, count);
        } catch (ArrayIndexOutOfBoundsException x) {
            Assert.fail(Helper.printStackTraceToString(x));
        }
    } finally {
        // Clean up
        beginTransaction(em);
        for (Employee employee : employeesToRemove) {
            employee = em.merge(employee);
            for (Project project : employee.getProjects()) {
                em.remove(em.merge(project));
            }
            em.remove(employee);
        }
        commitTransaction(em);
        closeEntityManager(em);
    }
}
Also used : JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) 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) 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) Expression(org.eclipse.persistence.expressions.Expression) HashSet(java.util.HashSet)

Example 64 with Project

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

the class AdvancedJPAJunitTest method testProjectToEmployeeWithBatchFetchJoinFetch.

/**
 * Bug 400022
 * Test batch fetch with join fetch with batching on a M:M
 */
public void testProjectToEmployeeWithBatchFetchJoinFetch() {
    clearCache();
    EntityManager em = createEntityManager();
    beginTransaction(em);
    // Count SQL.
    QuerySQLTracker counter = new QuerySQLTracker(getServerSession());
    try {
        Query query = em.createQuery("SELECT p FROM Project p", Project.class);
        query.setHint(QueryHints.BATCH, "p.teamMembers");
        query.setHint(QueryHints.BATCH, "p.teamMembers.projects");
        query.setHint(QueryHints.BATCH, "p.teamMembers.phoneNumbers.owner");
        query.setHint(QueryHints.BATCH_TYPE, BatchFetchType.IN);
        query.setHint(QueryHints.FETCH, "p.teamMembers.address");
        query.setHint(QueryHints.FETCH, "p.teamMembers.phoneNumbers");
        List<Project> results = query.getResultList();
        // XXX - size here may vary depending on the model
        if (isWeavingEnabled() && counter.getSqlStatements().size() > 9) {
            fail("Should have been 9 queries but was: " + counter.getSqlStatements().size());
        }
        for (Project project : results) {
            assertNotNull("Project cannot be null", project);
            Employee employee = project.getTeamLeader();
            if (employee != null) {
                employee.getProjects().size();
            }
        }
        if (isWeavingEnabled() && counter.getSqlStatements().size() > 18) {
            fail("Should have been 18 queries but was: " + counter.getSqlStatements().size());
        }
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
        if (counter != null) {
            counter.remove();
        }
    }
}
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) 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) Query(jakarta.persistence.Query) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) DoesExistQuery(org.eclipse.persistence.queries.DoesExistQuery) TypedQuery(jakarta.persistence.TypedQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker)

Example 65 with Project

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

the class JUnitJPQLSimpleTestSuite method selectDirectCollectionNonMemberNestedTest.

public void selectDirectCollectionNonMemberNestedTest() {
    EntityManager em = createEntityManager();
    String all = "SELECT p FROM Project p WHERE p.teamLeader IS NOT NULL";
    List<Project> allProjectsWithTeamLeader = em.createQuery(all).getResultList();
    Assert.assertTrue("No projects with team leaders.", !allProjectsWithTeamLeader.isEmpty());
    String responsibility = null;
    for (Project project : allProjectsWithTeamLeader) {
        if (project.getTeamLeader().getResponsibilities().size() > 0) {
            responsibility = (String) project.getTeamLeader().getResponsibilities().iterator().next();
            break;
        }
    }
    Assert.assertTrue("Not a single teamLeader has any responsibilities!", responsibility != null);
    String ejbqlString1 = "SELECT p FROM Project p WHERE p.teamLeader IS NOT NULL AND ?1 MEMBER OF p.teamLeader.responsibilities";
    List result1 = em.createQuery(ejbqlString1).setParameter("1", responsibility).getResultList();
    Assert.assertTrue("MEMBER OF result is empty", !result1.isEmpty());
    String ejbqlString2 = "SELECT p FROM Project p WHERE p.teamLeader IS NOT NULL AND ?1 NOT MEMBER OF p.teamLeader.responsibilities";
    List result2 = em.createQuery(ejbqlString2).setParameter("1", responsibility).getResultList();
    Assert.assertTrue("NOT MEMBER OF result is empty", !result2.isEmpty());
    List union = new ArrayList(result1);
    union.addAll(result2);
    Assert.assertTrue("Union of results of MEMBER OF and NON MEMBER OF not equal to all projects with team leaders", comparer.compareObjects(union, allProjectsWithTeamLeader));
    for (int i = 0; i < result2.size(); i++) {
        if (result1.contains(result2.get(i))) {
            fail("results of MEMBER OF and NON MEMBER OF intersect");
        }
    }
}
Also used : 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) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Project (org.eclipse.persistence.testing.models.jpa.advanced.Project)65 EntityManager (jakarta.persistence.EntityManager)52 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)42 SmallProject (org.eclipse.persistence.testing.models.jpa.advanced.SmallProject)39 LargeProject (org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)38 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)31 HugeProject (org.eclipse.persistence.testing.models.jpa.advanced.HugeProject)25 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)19 SpecialHugeProject (org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject)18 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject)18 List (java.util.List)15 ArrayList (java.util.ArrayList)14 JpaCriteriaBuilder (org.eclipse.persistence.jpa.JpaCriteriaBuilder)12 Query (jakarta.persistence.Query)9 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)9 PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)8 PersistenceException (jakarta.persistence.PersistenceException)6 Predicate (jakarta.persistence.criteria.Predicate)6 TypedQuery (jakarta.persistence.TypedQuery)5 HashSet (java.util.HashSet)5