Search in sources :

Example 36 with EJBQueryImpl

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

the class CriteriaQueryTestSuite method testCriteriaDeleteCompareSQL.

public void testCriteriaDeleteCompareSQL() {
    EntityManager em = createEntityManager();
    JpaEntityManager jpaEM = JpaHelper.getEntityManager((EntityManager) em.getDelegate());
    EJBQueryImpl query = (EJBQueryImpl) jpaEM.createQuery("DELETE FROM PhoneNumber phone where phone.owner.firstName is not null");
    String baseSQL = query.getDatabaseQuery().getTranslatedSQLString(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    List baseSQLStrings = query.getDatabaseQuery().getTranslatedSQLStrings(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    // test query "Delete Employee e where e.firstName is not null";
    CriteriaBuilder qb = jpaEM.getCriteriaBuilder();
    CriteriaDelete<PhoneNumber> cq = qb.createCriteriaDelete(PhoneNumber.class);
    Root<PhoneNumber> root = cq.from(PhoneNumber.class);
    cq.where(qb.isNotNull(root.get("owner").get("firstName")));
    EJBQueryImpl testQuery = (EJBQueryImpl) jpaEM.createQuery(cq);
    String testSQL = testQuery.getDatabaseQuery().getTranslatedSQLString(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    List testSQLStrings = testQuery.getDatabaseQuery().getTranslatedSQLStrings(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    closeEntityManager(em);
    if (testSQL != null) {
        assertEquals("Delete Criteria query did not match SQL used for a JPQL query; generated SQL was: \"" + testSQL + "\"  but we expected: \"" + baseSQL + "\"", testSQL, baseSQL);
    } else {
        // check list of strings instead
        boolean pass = true;
        if (testSQLStrings == null || baseSQLStrings == null || testSQLStrings.size() != baseSQLStrings.size()) {
            pass = false;
        } else {
            List clonedBaseStrings = new java.util.ArrayList(baseSQLStrings);
            for (String testSQLString : (List<String>) testSQLStrings) {
                if (clonedBaseStrings.contains(testSQLString)) {
                    clonedBaseStrings.remove(testSQLString);
                } else {
                    pass = false;
                    break;
                }
            }
            if (clonedBaseStrings.size() != 0) {
                pass = false;
            }
        }
        assertTrue("Delete Criteria query translated strings did not match JPQL query; criteria generated " + testSQLStrings + " but JPQL generated " + baseSQLStrings, pass);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) PhoneNumber(org.eclipse.persistence.testing.models.jpa22.advanced.PhoneNumber) List(java.util.List)

Example 37 with EJBQueryImpl

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

the class AbstractQueryResource method namedQueryInternal.

/**
 * Executes given named query.
 *
 * @param version the version
 * @param persistenceUnit the persistence unit
 * @param queryName named query to execute
 * @param headers the http headers
 * @param uriInfo the uri info
 * @return the response
 */
protected Response namedQueryInternal(String version, String persistenceUnit, String queryName, HttpHeaders headers, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "namedQueryInternal", new Object[] { "GET", version, persistenceUnit, queryName, uriInfo.getRequestUri().toASCIIString() });
    try {
        final PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
        final Query query = context.buildQuery(getMatrixParameters(uriInfo, persistenceUnit), queryName, getMatrixParameters(uriInfo, queryName), getQueryParameters(uriInfo));
        final DatabaseQuery dbQuery = ((EJBQueryImpl<?>) query).getDatabaseQuery();
        final FeatureSet featureSet = context.getSupportedFeatureSet();
        final Response response;
        if (featureSet.isSupported(Feature.PAGING)) {
            response = processPageableQuery(context, queryName, dbQuery, query, headers, uriInfo);
        } else {
            response = namedQueryResponse(context, queryName, dbQuery, query, headers, uriInfo, featureSet.getResponseBuilder(Feature.NO_PAGING));
        }
        return response;
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    }
}
Also used : Response(jakarta.ws.rs.core.Response) ReportQuery(org.eclipse.persistence.queries.ReportQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Query(jakarta.persistence.Query) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) FeatureSet(org.eclipse.persistence.jpa.rs.features.FeatureSet) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException)

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