Search in sources :

Example 1 with ScrollableCursorPolicy

use of org.eclipse.persistence.queries.ScrollableCursorPolicy in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testQueryHints.

public void testQueryHints() {
    EntityManager em = (EntityManager) getEntityManagerFactory().createEntityManager().getDelegate();
    Query query = em.createQuery("SELECT OBJECT(e) FROM Employee e WHERE e.firstName = 'testQueryHints'");
    // Set a hint first to trigger query clone (because query is accessed before other hints are set).
    query.setHint(QueryHints.READ_ONLY, false);
    ObjectLevelReadQuery olrQuery = (ObjectLevelReadQuery) ((JpaQuery) 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());
    query.setHint(QueryHints.INDIRECTION_POLICY, CacheUsageIndirectionPolicy.Trigger);
    assertTrue("INDIRECTION_POLICY not set.", olrQuery.getInMemoryQueryIndirectionPolicyState() == InMemoryQueryIndirectionPolicy.SHOULD_TRIGGER_INDIRECTION);
    // 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());
    // Read-only
    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());
    // Maintain cache
    query.setHint(QueryHints.MAINTAIN_CACHE, true);
    assertTrue("MAINTAIN_CACHE set.", olrQuery.shouldMaintainCache());
    query.setHint(QueryHints.MAINTAIN_CACHE, "false");
    assertFalse("MAINTAIN_CACHE not set.", olrQuery.shouldMaintainCache());
    query.setHint(QueryHints.MAINTAIN_CACHE, Boolean.TRUE);
    assertTrue("MAINTAIN_CACHE not set.", olrQuery.shouldMaintainCache());
    query.setHint(QueryHints.MAINTAIN_CACHE, Boolean.FALSE);
    assertFalse("MAINTAIN_CACHE not set.", olrQuery.shouldMaintainCache());
    // Prepare
    query.setHint(QueryHints.PREPARE, true);
    assertTrue("PREPARE set.", olrQuery.shouldPrepare());
    query.setHint(QueryHints.PREPARE, "false");
    assertFalse("PREPARE not set.", olrQuery.shouldPrepare());
    query.setHint(QueryHints.PREPARE, Boolean.TRUE);
    assertTrue("PREPARE not set.", olrQuery.shouldPrepare());
    query.setHint(QueryHints.PREPARE, Boolean.FALSE);
    assertFalse("PREPARE not set.", olrQuery.shouldPrepare());
    // Cache statement
    query.setHint(QueryHints.CACHE_STATMENT, true);
    assertTrue("CACHE_STATMENT set.", olrQuery.shouldCacheStatement());
    query.setHint(QueryHints.CACHE_STATMENT, "false");
    assertFalse("CACHE_STATMENT not set.", olrQuery.shouldCacheStatement());
    query.setHint(QueryHints.CACHE_STATMENT, Boolean.TRUE);
    assertTrue("CACHE_STATMENT not set.", olrQuery.shouldCacheStatement());
    query.setHint(QueryHints.CACHE_STATMENT, Boolean.FALSE);
    assertFalse("CACHE_STATMENT not set.", olrQuery.shouldCacheStatement());
    // Flush
    query.setHint(QueryHints.FLUSH, true);
    assertTrue("FLUSH set.", olrQuery.getFlushOnExecute());
    query.setHint(QueryHints.FLUSH, "false");
    assertFalse("FLUSH not set.", olrQuery.getFlushOnExecute());
    query.setHint(QueryHints.FLUSH, Boolean.TRUE);
    assertTrue("FLUSH not set.", olrQuery.getFlushOnExecute());
    query.setHint(QueryHints.FLUSH, Boolean.FALSE);
    assertFalse("FLUSH not set.", olrQuery.getFlushOnExecute());
    // Native connection
    query.setHint(QueryHints.NATIVE_CONNECTION, true);
    assertTrue("NATIVE_CONNECTION set.", olrQuery.isNativeConnectionRequired());
    query.setHint(QueryHints.NATIVE_CONNECTION, "false");
    assertFalse("NATIVE_CONNECTION not set.", olrQuery.isNativeConnectionRequired());
    query.setHint(QueryHints.NATIVE_CONNECTION, Boolean.TRUE);
    assertTrue("NATIVE_CONNECTION not set.", olrQuery.isNativeConnectionRequired());
    query.setHint(QueryHints.NATIVE_CONNECTION, Boolean.FALSE);
    assertFalse("NATIVE_CONNECTION not set.", olrQuery.isNativeConnectionRequired());
    // Hint
    query.setHint(QueryHints.HINT, "/* use the index man */");
    assertTrue("HINT not set.", olrQuery.getHintString().equals("/* use the index man */"));
    // Cursor
    query.setHint(QueryHints.CURSOR, Boolean.TRUE);
    assertTrue("CURSOR not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().isCursoredStreamPolicy());
    query.setHint(QueryHints.CURSOR, Boolean.FALSE);
    assertFalse("CURSOR set.", ((ReadAllQuery) olrQuery).getContainerPolicy().isCursoredStreamPolicy());
    query.setHint(QueryHints.CURSOR_INITIAL_SIZE, "100");
    assertTrue("CURSOR not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().isCursoredStreamPolicy());
    assertTrue("CURSOR_INITIAL_SIZE not set.", ((CursoredStreamPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getInitialReadSize() == 100);
    query.setHint(QueryHints.CURSOR_INITIAL_SIZE, 200);
    assertTrue("CURSOR_INITIAL_SIZE not set.", ((CursoredStreamPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getInitialReadSize() == 200);
    query.setHint(QueryHints.CURSOR, Boolean.FALSE);
    query.setHint(QueryHints.CURSOR_PAGE_SIZE, "100");
    assertTrue("CURSOR not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().isCursoredStreamPolicy());
    assertTrue("CURSOR_PAGE_SIZE not set.", ((CursoredStreamPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getPageSize() == 100);
    query.setHint(QueryHints.CURSOR_PAGE_SIZE, 200);
    assertTrue("CURSOR_PAGE_SIZE not set.", ((CursoredStreamPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getPageSize() == 200);
    query.setHint(QueryHints.CURSOR, Boolean.FALSE);
    query.setHint(QueryHints.CURSOR_SIZE, "Select Count(*) from Employee");
    assertTrue("CURSOR_SIZE not set.", ((CursoredStreamPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getSizeQuery().getSQLString().equals("Select Count(*) from Employee"));
    // Scrollable cursor
    query.setHint(QueryHints.SCROLLABLE_CURSOR, Boolean.TRUE);
    assertTrue("SCROLLABLE_CURSOR not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().isScrollableCursorPolicy());
    query.setHint(QueryHints.SCROLLABLE_CURSOR, Boolean.FALSE);
    assertFalse("SCROLLABLE_CURSOR set.", ((ReadAllQuery) olrQuery).getContainerPolicy().isScrollableCursorPolicy());
    query.setHint(QueryHints.RESULT_SET_TYPE, ResultSetType.Reverse);
    assertTrue("RESULT_SET_TYPE not set.", ((ScrollableCursorPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getResultSetType() == ScrollableCursorPolicy.FETCH_REVERSE);
    query.setHint(QueryHints.RESULT_SET_CONCURRENCY, ResultSetConcurrency.Updatable);
    assertTrue("RESULT_SET_CONCURRENCY not set.", ((ScrollableCursorPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getResultSetConcurrency() == ScrollableCursorPolicy.CONCUR_UPDATABLE);
    // Exclusive connection
    query.setHint(QueryHints.EXCLUSIVE_CONNECTION, Boolean.TRUE);
    assertTrue("EXCLUSIVE_CONNECTION not set.", olrQuery.shouldUseExclusiveConnection());
    // Inheritance
    query.setHint(QueryHints.INHERITANCE_OUTER_JOIN, Boolean.TRUE);
    assertTrue("INHERITANCE_OUTER_JOIN not set.", olrQuery.shouldOuterJoinSubclasses());
    // History
    query.setHint(QueryHints.AS_OF, "1973/10/11 12:00:00");
    assertTrue("AS_OF not set.", olrQuery.getAsOfClause() != null);
    query.setHint(QueryHints.AS_OF_SCN, "12345");
    assertTrue("AS_OF_SCN not set.", ((Number) olrQuery.getAsOfClause().getValue()).intValue() == 12345);
    // Fetch groups
    query.setHint(QueryHints.FETCH_GROUP_DEFAULT, Boolean.FALSE);
    assertFalse("FETCH_GROUP_DEFAULT not set.", olrQuery.shouldUseDefaultFetchGroup());
    query.setHint(QueryHints.FETCH_GROUP_NAME, "nameAndCity");
    assertTrue("FETCH_GROUP_NAME not set.", olrQuery.getFetchGroupName().equals("nameAndCity"));
    query.setHint(QueryHints.FETCH_GROUP_ATTRIBUTE, "firstName");
    query.setHint(QueryHints.FETCH_GROUP_ATTRIBUTE, "lastName");
    assertTrue("FETCH_GROUP_ATTRIBUTE not set.", olrQuery.getFetchGroup().containsAttribute("firstName"));
    assertTrue("FETCH_GROUP_ATTRIBUTE not set.", olrQuery.getFetchGroup().containsAttribute("lastName"));
    FetchGroup fetchGroup = new FetchGroup();
    fetchGroup.addAttribute("id");
    query.setHint(QueryHints.FETCH_GROUP, fetchGroup);
    assertTrue("FETCH_GROUP not set.", olrQuery.getFetchGroup() == fetchGroup);
    // Timeout
    query.setHint(QueryHints.JDBC_TIMEOUT, 100);
    assertTrue("Timeout not set.", olrQuery.getQueryTimeout() == 100);
    // JDBC
    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.JDBC_FIRST_RESULT, 123);
    assertTrue("JDBC_FIRST_RESULT not set.", olrQuery.getFirstResult() == 123);
    // Refresh
    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);
    // Result collection
    query.setHint(QueryHints.RESULT_COLLECTION_TYPE, java.util.ArrayList.class);
    assertTrue("ArrayList not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().getContainerClass().equals(java.util.ArrayList.class));
    query.setHint(QueryHints.RESULT_COLLECTION_TYPE, "java.util.Vector");
    assertTrue("Vector not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().getContainerClass().equals(java.util.Vector.class));
    query.setHint(QueryHints.RESULT_COLLECTION_TYPE, "org.eclipse.persistence.testing.tests.jpa.composite.advanced.EntityManagerJUnitTestSuite$CustomerCollection");
    assertTrue("CustomerCollection not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().getContainerClass().equals(CustomerCollection.class));
    // Query type
    query.setHint(QueryHints.QUERY_TYPE, QueryType.ReadObject);
    assertTrue("QUERY_TYPE not set.", ((JpaQuery) query).getDatabaseQuery().getClass().equals(ReadObjectQuery.class));
    query.setHint(QueryHints.QUERY_TYPE, QueryType.Report);
    assertTrue("QUERY_TYPE not set.", ((JpaQuery) query).getDatabaseQuery().getClass().equals(ReportQuery.class));
    query.setHint(QueryHints.QUERY_TYPE, QueryType.DataModify);
    assertTrue("QUERY_TYPE not set.", ((JpaQuery) query).getDatabaseQuery().getClass().equals(DataModifyQuery.class));
    query.setHint(QueryHints.QUERY_TYPE, "org.eclipse.persistence.queries.ValueReadQuery");
    assertTrue("QUERY_TYPE not set.", ((JpaQuery) query).getDatabaseQuery().getClass().equals(ValueReadQuery.class));
    query.setHint(QueryHints.QUERY_TYPE, QueryType.ReadAll);
    assertTrue("QUERY_TYPE not set.", ((JpaQuery) query).getDatabaseQuery().getClass().equals(ReadAllQuery.class));
    // Result type
    query.setHint(QueryHints.QUERY_TYPE, QueryType.Report);
    query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
    assertTrue("RESULT_TYPE not set.", ((ReportQuery) ((JpaQuery) query).getDatabaseQuery()).getReturnType() == ReportQuery.ShouldReturnReportResult);
    query.setHint(QueryHints.RESULT_TYPE, ResultType.Array);
    assertTrue("RESULT_TYPE not set.", ((ReportQuery) ((JpaQuery) query).getDatabaseQuery()).getReturnType() == ReportQuery.ShouldReturnArray);
    query.setHint(QueryHints.RESULT_TYPE, ResultType.Value);
    assertTrue("RESULT_TYPE not set.", ((ReportQuery) ((JpaQuery) query).getDatabaseQuery()).getReturnType() == ReportQuery.ShouldReturnSingleValue);
    query.setHint(QueryHints.QUERY_TYPE, QueryType.DataRead);
    query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
    query.setHint(QueryHints.RESULT_TYPE, ResultType.Array);
    query.setHint(QueryHints.RESULT_TYPE, ResultType.Value);
    closeEntityManager(em);
}
Also used : Query(jakarta.persistence.Query) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) ReportQuery(org.eclipse.persistence.queries.ReportQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ValueReadQuery(org.eclipse.persistence.queries.ValueReadQuery) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery) TypedQuery(jakarta.persistence.TypedQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) ReportQuery(org.eclipse.persistence.queries.ReportQuery) ValueReadQuery(org.eclipse.persistence.queries.ValueReadQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ArrayList(java.util.ArrayList) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) ScrollableCursorPolicy(org.eclipse.persistence.queries.ScrollableCursorPolicy) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) PhoneNumber(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.PhoneNumber) FetchGroup(org.eclipse.persistence.queries.FetchGroup) CursoredStreamPolicy(org.eclipse.persistence.queries.CursoredStreamPolicy) Vector(java.util.Vector) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery)

Example 2 with ScrollableCursorPolicy

use of org.eclipse.persistence.queries.ScrollableCursorPolicy in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testQueryHints.

public void testQueryHints() {
    EntityManager em = (EntityManager) getEntityManagerFactory().createEntityManager().getDelegate();
    Query query = em.createQuery("SELECT OBJECT(e) FROM Employee e WHERE e.firstName = 'testQueryHints'");
    // Set a hint first to trigger query clone (because query is accessed before other hints are set).
    query.setHint(QueryHints.READ_ONLY, false);
    ObjectLevelReadQuery olrQuery = (ObjectLevelReadQuery) ((JpaQuery) 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());
    query.setHint(QueryHints.INDIRECTION_POLICY, CacheUsageIndirectionPolicy.Trigger);
    assertTrue("INDIRECTION_POLICY not set.", olrQuery.getInMemoryQueryIndirectionPolicyState() == InMemoryQueryIndirectionPolicy.SHOULD_TRIGGER_INDIRECTION);
    // 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());
    // Read-only
    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());
    // Maintain cache
    query.setHint(QueryHints.MAINTAIN_CACHE, true);
    assertTrue("MAINTAIN_CACHE set.", olrQuery.shouldMaintainCache());
    query.setHint(QueryHints.MAINTAIN_CACHE, "false");
    assertFalse("MAINTAIN_CACHE not set.", olrQuery.shouldMaintainCache());
    query.setHint(QueryHints.MAINTAIN_CACHE, Boolean.TRUE);
    assertTrue("MAINTAIN_CACHE not set.", olrQuery.shouldMaintainCache());
    query.setHint(QueryHints.MAINTAIN_CACHE, Boolean.FALSE);
    assertFalse("MAINTAIN_CACHE not set.", olrQuery.shouldMaintainCache());
    // Prepare
    query.setHint(QueryHints.PREPARE, true);
    assertTrue("PREPARE set.", olrQuery.shouldPrepare());
    query.setHint(QueryHints.PREPARE, "false");
    assertFalse("PREPARE not set.", olrQuery.shouldPrepare());
    query.setHint(QueryHints.PREPARE, Boolean.TRUE);
    assertTrue("PREPARE not set.", olrQuery.shouldPrepare());
    query.setHint(QueryHints.PREPARE, Boolean.FALSE);
    assertFalse("PREPARE not set.", olrQuery.shouldPrepare());
    // Cache statement
    query.setHint(QueryHints.CACHE_STATMENT, true);
    assertTrue("CACHE_STATMENT set.", olrQuery.shouldCacheStatement());
    query.setHint(QueryHints.CACHE_STATMENT, "false");
    assertFalse("CACHE_STATMENT not set.", olrQuery.shouldCacheStatement());
    query.setHint(QueryHints.CACHE_STATMENT, Boolean.TRUE);
    assertTrue("CACHE_STATMENT not set.", olrQuery.shouldCacheStatement());
    query.setHint(QueryHints.CACHE_STATMENT, Boolean.FALSE);
    assertFalse("CACHE_STATMENT not set.", olrQuery.shouldCacheStatement());
    // Flush
    query.setHint(QueryHints.FLUSH, true);
    assertTrue("FLUSH set.", olrQuery.getFlushOnExecute());
    query.setHint(QueryHints.FLUSH, "false");
    assertFalse("FLUSH not set.", olrQuery.getFlushOnExecute());
    query.setHint(QueryHints.FLUSH, Boolean.TRUE);
    assertTrue("FLUSH not set.", olrQuery.getFlushOnExecute());
    query.setHint(QueryHints.FLUSH, Boolean.FALSE);
    assertFalse("FLUSH not set.", olrQuery.getFlushOnExecute());
    // Native connection
    query.setHint(QueryHints.NATIVE_CONNECTION, true);
    assertTrue("NATIVE_CONNECTION set.", olrQuery.isNativeConnectionRequired());
    query.setHint(QueryHints.NATIVE_CONNECTION, "false");
    assertFalse("NATIVE_CONNECTION not set.", olrQuery.isNativeConnectionRequired());
    query.setHint(QueryHints.NATIVE_CONNECTION, Boolean.TRUE);
    assertTrue("NATIVE_CONNECTION not set.", olrQuery.isNativeConnectionRequired());
    query.setHint(QueryHints.NATIVE_CONNECTION, Boolean.FALSE);
    assertFalse("NATIVE_CONNECTION not set.", olrQuery.isNativeConnectionRequired());
    // Hint
    query.setHint(QueryHints.HINT, "/* use the index man */");
    assertTrue("HINT not set.", olrQuery.getHintString().equals("/* use the index man */"));
    // Cursor
    query.setHint(QueryHints.CURSOR, Boolean.TRUE);
    assertTrue("CURSOR not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().isCursoredStreamPolicy());
    query.setHint(QueryHints.CURSOR, Boolean.FALSE);
    assertFalse("CURSOR set.", ((ReadAllQuery) olrQuery).getContainerPolicy().isCursoredStreamPolicy());
    query.setHint(QueryHints.CURSOR_INITIAL_SIZE, "100");
    assertTrue("CURSOR not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().isCursoredStreamPolicy());
    assertTrue("CURSOR_INITIAL_SIZE not set.", ((CursoredStreamPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getInitialReadSize() == 100);
    query.setHint(QueryHints.CURSOR_INITIAL_SIZE, 200);
    assertTrue("CURSOR_INITIAL_SIZE not set.", ((CursoredStreamPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getInitialReadSize() == 200);
    query.setHint(QueryHints.CURSOR, Boolean.FALSE);
    query.setHint(QueryHints.CURSOR_PAGE_SIZE, "100");
    assertTrue("CURSOR not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().isCursoredStreamPolicy());
    assertTrue("CURSOR_PAGE_SIZE not set.", ((CursoredStreamPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getPageSize() == 100);
    query.setHint(QueryHints.CURSOR_PAGE_SIZE, 200);
    assertTrue("CURSOR_PAGE_SIZE not set.", ((CursoredStreamPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getPageSize() == 200);
    query.setHint(QueryHints.CURSOR, Boolean.FALSE);
    query.setHint(QueryHints.CURSOR_SIZE, "Select Count(*) from Employee");
    assertTrue("CURSOR_SIZE not set.", ((CursoredStreamPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getSizeQuery().getSQLString().equals("Select Count(*) from Employee"));
    // Scrollable cursor
    query.setHint(QueryHints.SCROLLABLE_CURSOR, Boolean.TRUE);
    assertTrue("SCROLLABLE_CURSOR not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().isScrollableCursorPolicy());
    query.setHint(QueryHints.SCROLLABLE_CURSOR, Boolean.FALSE);
    assertFalse("SCROLLABLE_CURSOR set.", ((ReadAllQuery) olrQuery).getContainerPolicy().isScrollableCursorPolicy());
    query.setHint(QueryHints.RESULT_SET_TYPE, ResultSetType.Reverse);
    assertTrue("RESULT_SET_TYPE not set.", ((ScrollableCursorPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getResultSetType() == ScrollableCursorPolicy.FETCH_REVERSE);
    query.setHint(QueryHints.RESULT_SET_CONCURRENCY, ResultSetConcurrency.Updatable);
    assertTrue("RESULT_SET_CONCURRENCY not set.", ((ScrollableCursorPolicy) ((ReadAllQuery) olrQuery).getContainerPolicy()).getResultSetConcurrency() == ScrollableCursorPolicy.CONCUR_UPDATABLE);
    // Exclusive connection
    query.setHint(QueryHints.EXCLUSIVE_CONNECTION, Boolean.TRUE);
    assertTrue("EXCLUSIVE_CONNECTION not set.", olrQuery.shouldUseExclusiveConnection());
    // Inheritance
    query.setHint(QueryHints.INHERITANCE_OUTER_JOIN, Boolean.TRUE);
    assertTrue("INHERITANCE_OUTER_JOIN not set.", olrQuery.shouldOuterJoinSubclasses());
    // History
    query.setHint(QueryHints.AS_OF, "1973/10/11 12:00:00");
    assertTrue("AS_OF not set.", olrQuery.getAsOfClause() != null);
    query.setHint(QueryHints.AS_OF_SCN, "12345");
    assertTrue("AS_OF_SCN not set.", ((Number) olrQuery.getAsOfClause().getValue()).intValue() == 12345);
    // Fetch groups
    query.setHint(QueryHints.FETCH_GROUP_DEFAULT, Boolean.FALSE);
    assertFalse("FETCH_GROUP_DEFAULT not set.", olrQuery.shouldUseDefaultFetchGroup());
    query.setHint(QueryHints.FETCH_GROUP_NAME, "nameAndCity");
    assertTrue("FETCH_GROUP_NAME not set.", olrQuery.getFetchGroupName().equals("nameAndCity"));
    query.setHint(QueryHints.FETCH_GROUP_ATTRIBUTE, "firstName");
    query.setHint(QueryHints.FETCH_GROUP_ATTRIBUTE, "lastName");
    assertTrue("FETCH_GROUP_ATTRIBUTE not set.", olrQuery.getFetchGroup().containsAttribute("firstName"));
    assertTrue("FETCH_GROUP_ATTRIBUTE not set.", olrQuery.getFetchGroup().containsAttribute("lastName"));
    FetchGroup fetchGroup = new FetchGroup();
    fetchGroup.addAttribute("id");
    query.setHint(QueryHints.FETCH_GROUP, fetchGroup);
    assertTrue("FETCH_GROUP not set.", olrQuery.getFetchGroup() == fetchGroup);
    // Timeout
    query.setHint(QueryHints.JDBC_TIMEOUT, 100);
    assertTrue("Timeout not set.", olrQuery.getQueryTimeout() == 100);
    // JDBC
    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.JDBC_FIRST_RESULT, 123);
    assertTrue("JDBC_FIRST_RESULT not set.", olrQuery.getFirstResult() == 123);
    // Refresh
    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);
    // Result collection
    query.setHint(QueryHints.RESULT_COLLECTION_TYPE, java.util.ArrayList.class);
    assertTrue("ArrayList not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().getContainerClass().equals(java.util.ArrayList.class));
    query.setHint(QueryHints.RESULT_COLLECTION_TYPE, "java.util.Vector");
    assertTrue("Vector not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().getContainerClass().equals(java.util.Vector.class));
    query.setHint(QueryHints.RESULT_COLLECTION_TYPE, "org.eclipse.persistence.testing.models.jpa.relationships.CustomerCollection");
    assertTrue("CustomerCollection not set.", ((ReadAllQuery) olrQuery).getContainerPolicy().getContainerClass().equals(CustomerCollection.class));
    // Query type
    query.setHint(QueryHints.QUERY_TYPE, QueryType.ReadObject);
    assertTrue("QUERY_TYPE not set.", ((JpaQuery) query).getDatabaseQuery().getClass().equals(ReadObjectQuery.class));
    query.setHint(QueryHints.QUERY_TYPE, QueryType.Report);
    assertTrue("QUERY_TYPE not set.", ((JpaQuery) query).getDatabaseQuery().getClass().equals(ReportQuery.class));
    query.setHint(QueryHints.QUERY_TYPE, QueryType.DataModify);
    assertTrue("QUERY_TYPE not set.", ((JpaQuery) query).getDatabaseQuery().getClass().equals(DataModifyQuery.class));
    query.setHint(QueryHints.QUERY_TYPE, "org.eclipse.persistence.queries.ValueReadQuery");
    assertTrue("QUERY_TYPE not set.", ((JpaQuery) query).getDatabaseQuery().getClass().equals(ValueReadQuery.class));
    query.setHint(QueryHints.QUERY_TYPE, QueryType.ReadAll);
    assertTrue("QUERY_TYPE not set.", ((JpaQuery) query).getDatabaseQuery().getClass().equals(ReadAllQuery.class));
    // Result type
    query.setHint(QueryHints.QUERY_TYPE, QueryType.Report);
    query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
    assertTrue("RESULT_TYPE not set.", ((ReportQuery) ((JpaQuery) query).getDatabaseQuery()).getReturnType() == ReportQuery.ShouldReturnReportResult);
    query.setHint(QueryHints.RESULT_TYPE, ResultType.Array);
    assertTrue("RESULT_TYPE not set.", ((ReportQuery) ((JpaQuery) query).getDatabaseQuery()).getReturnType() == ReportQuery.ShouldReturnArray);
    query.setHint(QueryHints.RESULT_TYPE, ResultType.Value);
    assertTrue("RESULT_TYPE not set.", ((ReportQuery) ((JpaQuery) query).getDatabaseQuery()).getReturnType() == ReportQuery.ShouldReturnSingleValue);
    query.setHint(QueryHints.QUERY_TYPE, QueryType.DataRead);
    query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
    query.setHint(QueryHints.RESULT_TYPE, ResultType.Array);
    query.setHint(QueryHints.RESULT_TYPE, ResultType.Value);
    closeEntityManager(em);
}
Also used : 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) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) ReportQuery(org.eclipse.persistence.queries.ReportQuery) ValueReadQuery(org.eclipse.persistence.queries.ValueReadQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ArrayList(java.util.ArrayList) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) ScrollableCursorPolicy(org.eclipse.persistence.queries.ScrollableCursorPolicy) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) CustomerCollection(org.eclipse.persistence.testing.models.jpa.relationships.CustomerCollection) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) FetchGroup(org.eclipse.persistence.queries.FetchGroup) CursoredStreamPolicy(org.eclipse.persistence.queries.CursoredStreamPolicy) Vector(java.util.Vector) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery)

Aggregations

EntityManager (jakarta.persistence.EntityManager)2 Query (jakarta.persistence.Query)2 TypedQuery (jakarta.persistence.TypedQuery)2 ArrayList (java.util.ArrayList)2 Vector (java.util.Vector)2 EJBQueryImpl (org.eclipse.persistence.internal.jpa.EJBQueryImpl)2 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)2 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)2 CursoredStreamPolicy (org.eclipse.persistence.queries.CursoredStreamPolicy)2 DataModifyQuery (org.eclipse.persistence.queries.DataModifyQuery)2 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)2 FetchGroup (org.eclipse.persistence.queries.FetchGroup)2 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)2 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)2 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)2 ReportQuery (org.eclipse.persistence.queries.ReportQuery)2 ScrollableCursorPolicy (org.eclipse.persistence.queries.ScrollableCursorPolicy)2 ValueReadQuery (org.eclipse.persistence.queries.ValueReadQuery)2 PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)1 PhoneNumber (org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.PhoneNumber)1