Search in sources :

Example 16 with EJBQueryImpl

use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.

the class AdvancedJPAJunitTest method testEmployeeToProjectWithBatchFetchTypeInReverseIteration.

/**
 * Bug 412056
 * Test batch fetch with size smaller than results in reverse order
 */
public void testEmployeeToProjectWithBatchFetchTypeInReverseIteration() {
    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
        Collections.reverse(employees);
        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 17 with EJBQueryImpl

use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.

the class AdvancedJPAJunitTest method testRemoveJillWithPrivateOwnedPhoneNumbers.

/**
 * Tests a @PrivateOwned @OneToMany mapping.
 */
public void testRemoveJillWithPrivateOwnedPhoneNumbers() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        EJBQueryImpl query = (EJBQueryImpl) em.createNamedQuery("findAllEmployeesByFirstName");
        query.setParameter("firstname", "Jill");
        Collection<Employee> employees = query.getResultCollection();
        if (employees.isEmpty()) {
            fail("No employees named Jill were found. Test requires at least one Jill to be created in the EmployeePopulator.");
        } else {
            Employee jill = employees.iterator().next();
            Collection<PhoneNumber> phoneNumbers = jill.getPhoneNumbers();
            if (phoneNumbers.isEmpty()) {
                fail("Jill does not have any phone numbers. Test requires that Jill have atleast one phone number created in the EmployeePopulator.");
            }
            // Re-assign her managed employees and remove from her list.
            for (Employee employee : jill.getManagedEmployees()) {
                employee.setManager(jill.getManager());
            }
            jill.getManagedEmployees().clear();
            int jillId = jill.getId();
            em.remove(jill);
            commitTransaction(em);
            assertNull("Jill herself was not removed.", em.find(Employee.class, jillId));
            for (PhoneNumber phoneNumber : phoneNumbers) {
                assertNull("Jill's phone numbers were not deleted.", em.find(PhoneNumber.class, phoneNumber.buildPK()));
            }
        }
    } 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) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl)

Example 18 with EJBQueryImpl

use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testQueryHints.

public void testQueryHints() {
    EntityManager em = getEntityManagerFactory("fieldaccess").createEntityManager();
    Query query = em.createQuery("SELECT OBJECT(e) FROM Employee e WHERE e.firstName = 'testQueryHints'");
    ObjectLevelReadQuery olrQuery = (ObjectLevelReadQuery) ((EJBQueryImpl) query).getDatabaseQuery();
    // binding
    // original state = default state
    assertTrue(olrQuery.shouldIgnoreBindAllParameters());
    // set boolean true
    query.setHint(QueryHints.BIND_PARAMETERS, true);
    // Parse cached query may be cloned when hint set, so re-get.
    olrQuery = (ObjectLevelReadQuery) ((EJBQueryImpl) query).getDatabaseQuery();
    assertTrue("Binding not set.", olrQuery.shouldBindAllParameters());
    // reset to original state
    query.setHint(QueryHints.BIND_PARAMETERS, "");
    assertTrue("Binding not set.", olrQuery.shouldIgnoreBindAllParameters());
    // set "false"
    query.setHint(QueryHints.BIND_PARAMETERS, "false");
    assertFalse("Binding not set.", olrQuery.shouldBindAllParameters());
    // reset to the original state
    query.setHint(QueryHints.BIND_PARAMETERS, "");
    assertTrue("Binding not set.", olrQuery.shouldIgnoreBindAllParameters());
    // cache usage
    query.setHint(QueryHints.CACHE_USAGE, CacheUsage.DoNotCheckCache);
    assertTrue("Cache usage not set.", olrQuery.getCacheUsage() == ObjectLevelReadQuery.DoNotCheckCache);
    query.setHint(QueryHints.CACHE_USAGE, CacheUsage.CheckCacheOnly);
    assertTrue("Cache usage not set.", olrQuery.shouldCheckCacheOnly());
    query.setHint(QueryHints.CACHE_USAGE, CacheUsage.ConformResultsInUnitOfWork);
    assertTrue("Cache usage not set.", olrQuery.shouldConformResultsInUnitOfWork());
    // reset to the original state
    query.setHint(QueryHints.CACHE_USAGE, "");
    assertTrue("Cache usage not set.", olrQuery.shouldCheckDescriptorForCacheUsage());
    // pessimistic lock
    query.setHint(QueryHints.PESSIMISTIC_LOCK, PessimisticLock.Lock);
    assertTrue("Lock not set.", olrQuery.getLockMode() == ObjectLevelReadQuery.LOCK);
    query.setHint(QueryHints.PESSIMISTIC_LOCK, PessimisticLock.NoLock);
    assertTrue("Lock not set.", olrQuery.getLockMode() == ObjectLevelReadQuery.NO_LOCK);
    query.setHint(QueryHints.PESSIMISTIC_LOCK, PessimisticLock.LockNoWait);
    assertTrue("Lock not set.", olrQuery.getLockMode() == ObjectLevelReadQuery.LOCK_NOWAIT);
    // default state
    query.setHint(QueryHints.PESSIMISTIC_LOCK, "");
    assertTrue("Lock not set.", olrQuery.getLockMode() == ObjectLevelReadQuery.NO_LOCK);
    // refresh
    // set to original state - don't refresh.
    // the previously run LOCK and LOCK_NOWAIT have swithed it to true
    query.setHint(QueryHints.REFRESH, false);
    assertFalse("Refresh not set.", olrQuery.shouldRefreshIdentityMapResult());
    // set boolean true
    query.setHint(QueryHints.REFRESH, true);
    assertTrue("Refresh not set.", olrQuery.shouldRefreshIdentityMapResult());
    // check if cascade refresh is enabled
    assertTrue("CascadeByMapping not set.", olrQuery.shouldCascadeByMapping());
    // set "false"
    query.setHint(QueryHints.REFRESH, "false");
    assertFalse("Refresh not set.", olrQuery.shouldRefreshIdentityMapResult());
    // set Boolean.TRUE
    query.setHint(QueryHints.REFRESH, Boolean.TRUE);
    assertTrue("Refresh not set.", olrQuery.shouldRefreshIdentityMapResult());
    // check if cascade refresh is enabled
    assertTrue("CascadeByMapping not set.", olrQuery.shouldCascadeByMapping());
    // reset to original state
    query.setHint(QueryHints.REFRESH, "");
    assertFalse("Refresh not set.", olrQuery.shouldRefreshIdentityMapResult());
    query.setHint(QueryHints.READ_ONLY, "false");
    assertFalse("Read-only not set.", olrQuery.isReadOnly());
    query.setHint(QueryHints.READ_ONLY, Boolean.TRUE);
    assertTrue("Read-only not set.", olrQuery.isReadOnly());
    query.setHint(QueryHints.READ_ONLY, Boolean.FALSE);
    assertFalse("Read-only not set.", olrQuery.isReadOnly());
    query.setHint(QueryHints.JDBC_TIMEOUT, 100);
    assertTrue("Timeout not set.", olrQuery.getQueryTimeout() == 100);
    query.setHint(QueryHints.JDBC_FETCH_SIZE, 101);
    assertTrue("Fetch-size not set.", olrQuery.getFetchSize() == 101);
    query.setHint(QueryHints.JDBC_MAX_ROWS, 103);
    assertTrue("Max-rows not set.", olrQuery.getMaxRows() == 103);
    query.setHint(QueryHints.REFRESH_CASCADE, CascadePolicy.NoCascading);
    assertTrue(olrQuery.getCascadePolicy() == DatabaseQuery.NoCascading);
    query.setHint(QueryHints.REFRESH_CASCADE, CascadePolicy.CascadeByMapping);
    assertTrue(olrQuery.getCascadePolicy() == DatabaseQuery.CascadeByMapping);
    query.setHint(QueryHints.REFRESH_CASCADE, CascadePolicy.CascadeAllParts);
    assertTrue(olrQuery.getCascadePolicy() == DatabaseQuery.CascadeAllParts);
    query.setHint(QueryHints.REFRESH_CASCADE, CascadePolicy.CascadePrivateParts);
    assertTrue(olrQuery.getCascadePolicy() == DatabaseQuery.CascadePrivateParts);
    // reset to the original state
    query.setHint(QueryHints.REFRESH_CASCADE, "");
    assertTrue(olrQuery.getCascadePolicy() == DatabaseQuery.CascadeByMapping);
    query.setHint(QueryHints.RESULT_COLLECTION_TYPE, java.util.ArrayList.class);
    assertTrue("ArrayList not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().getContainerClassName().equals(java.util.ArrayList.class.getName()));
    query.setHint(QueryHints.RESULT_COLLECTION_TYPE, "java.util.Vector");
    assertTrue("Vector not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().getContainerClassName().equals(java.util.Vector.class.getName()));
    closeEntityManager(em);
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Query(jakarta.persistence.Query) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl)

Example 19 with EJBQueryImpl

use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.

the class EntityManagerTLRJUnitTestSuite method testQueryTimeOut.

public void testQueryTimeOut() {
    EntityManager em = getEntityManagerFactory("fieldaccess").createEntityManager();
    Query query = em.createQuery("SELECT d FROM Department d");
    ObjectLevelReadQuery olrQuery = (ObjectLevelReadQuery) ((EJBQueryImpl) query).getDatabaseQuery();
    // testing for query timeout specified in persistence.xml
    assertTrue("Timeout overriden or not set in persistence.xml", olrQuery.getQueryTimeout() == 100);
    query.setHint(QueryHints.JDBC_TIMEOUT, 500);
    olrQuery = (ObjectLevelReadQuery) ((EJBQueryImpl) query).getDatabaseQuery();
    assertTrue(olrQuery.getQueryTimeout() == 500);
    closeEntityManager(em);
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Query(jakarta.persistence.Query) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl)

Example 20 with EJBQueryImpl

use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testQueryTimeOut.

public void testQueryTimeOut() {
    if (getDatabaseSession().getPlatform().isPostgreSQL()) {
        warning("PostgreSQL does not support query timeout.");
        return;
    }
    EntityManager em = getEntityManagerFactory("fieldaccess").createEntityManager();
    Query query = em.createQuery("SELECT d FROM Department d");
    ObjectLevelReadQuery olrQuery = (ObjectLevelReadQuery) ((EJBQueryImpl) query).getDatabaseQuery();
    // testing for query timeout specified in persistence.xml
    assertTrue("Timeout overriden or not set in persistence.xml", olrQuery.getQueryTimeout() == 100000);
    query.setHint(QueryHints.JDBC_TIMEOUT, 500000);
    olrQuery = (ObjectLevelReadQuery) ((EJBQueryImpl) query).getDatabaseQuery();
    assertTrue(olrQuery.getQueryTimeout() == 500000);
    closeEntityManager(em);
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Query(jakarta.persistence.Query) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl)

Aggregations

EJBQueryImpl (org.eclipse.persistence.internal.jpa.EJBQueryImpl)37 EntityManager (jakarta.persistence.EntityManager)31 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)19 Query (jakarta.persistence.Query)16 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)15 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)12 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)8 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)8 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)6 MapContainerPolicy (org.eclipse.persistence.internal.queries.MapContainerPolicy)6 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)6 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)6 List (java.util.List)5 PersistenceException (jakarta.persistence.PersistenceException)4 Collection (java.util.Collection)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 QueryException (org.eclipse.persistence.exceptions.QueryException)4 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)4 ReportQuery (org.eclipse.persistence.queries.ReportQuery)4