Search in sources :

Example 1 with EJBQueryImpl

use of org.eclipse.persistence.internal.jpa.EJBQueryImpl in project cuba by cuba-platform.

the class QueryImpl method preExecute.

protected void preExecute(JpaQuery jpaQuery) {
    // copying behaviour of org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery()
    DatabaseQuery elDbQuery = ((EJBQueryImpl) jpaQuery).getDatabaseQueryInternal();
    boolean isObjectLevelReadQuery = elDbQuery.isObjectLevelReadQuery();
    if (jpaQuery.getFlushMode() == FlushModeType.AUTO && (!isObjectLevelReadQuery || !((ObjectLevelReadQuery) elDbQuery).isReadOnly())) {
        // flush is expected
        entityChangedEventManager.beforeFlush(support.getInstances(entityManager));
        support.processFlush(entityManager, true);
    }
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl)

Example 2 with EJBQueryImpl

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

the class PersistenceContext method buildQuery.

/**
 * Builds the query.
 *
 * @param tenantId the tenant id
 * @param name the name
 * @param parameters the parameters
 * @param hints the hints
 * @return the query
 */
public Query buildQuery(Map<String, String> tenantId, String name, Map<?, ?> parameters, Map<String, ?> hints) {
    EntityManager em = getEmf().createEntityManager(tenantId);
    Query query = em.createNamedQuery(name);
    DatabaseQuery dbQuery = ((EJBQueryImpl<?>) query).getDatabaseQuery();
    if (parameters != null) {
        Iterator<?> i = parameters.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
            String key = (String) entry.getKey();
            Class<?> parameterClass = null;
            int index = dbQuery.getArguments().indexOf(key);
            if (index >= 0) {
                parameterClass = dbQuery.getArgumentTypes().get(index);
            }
            Object parameter = entry.getValue();
            if (parameterClass != null) {
                parameter = ConversionManager.getDefaultManager().convertObject(parameter, parameterClass);
            }
            query.setParameter(key, parameter);
        }
    }
    if (hints != null) {
        for (Map.Entry<String, ?> entry : hints.entrySet()) {
            query.setHint(entry.getKey(), entry.getValue());
        }
    }
    return query;
}
Also used : Query(jakarta.persistence.Query) RestPageableQuery(org.eclipse.persistence.jpa.rs.annotations.RestPageableQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) EntityManager(jakarta.persistence.EntityManager) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with EJBQueryImpl

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

the class PersistenceContext method constructQuery.

protected Query constructQuery(EntityManager em, String name, Map<?, ?> parameters, Map<String, ?> hints) {
    Query query = em.createNamedQuery(name);
    DatabaseQuery dbQuery = ((EJBQueryImpl<?>) query).getDatabaseQuery();
    if (parameters != null) {
        Iterator<?> i = parameters.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
            String key = (String) entry.getKey();
            Class<?> parameterClass = null;
            int index = dbQuery.getArguments().indexOf(key);
            if (index >= 0) {
                parameterClass = dbQuery.getArgumentTypes().get(index);
            }
            Object parameter = entry.getValue();
            if (parameterClass != null) {
                parameter = ConversionManager.getDefaultManager().convertObject(parameter, parameterClass);
            }
            query.setParameter(key, parameter);
        }
    }
    if (hints != null) {
        for (Map.Entry<String, ?> entry : hints.entrySet()) {
            query.setHint(entry.getKey(), entry.getValue());
        }
    }
    return query;
}
Also used : Query(jakarta.persistence.Query) RestPageableQuery(org.eclipse.persistence.jpa.rs.annotations.RestPageableQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with EJBQueryImpl

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

the class AbstractSingleResultQueryResource method namedQuerySingleResultInternal.

/**
 * Named query single result.
 *
 * @param version the version
 * @param persistenceUnit the persistence unit
 * @param queryName the query name
 * @param headers the http headers
 * @param uriInfo the uriInfo
 * @return the response
 */
@SuppressWarnings("rawtypes")
protected Response namedQuerySingleResultInternal(String version, String persistenceUnit, String queryName, HttpHeaders headers, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "namedQuerySingleResultInternal", 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 FeatureResponseBuilder responseBuilder = context.getSupportedFeatureSet().getResponseBuilder(Feature.NO_PAGING);
        if (dbQuery instanceof ReportQuery) {
            final List<ReportItem> reportItems = ((ReportQuery) dbQuery).getItems();
            final Object queryResults = query.getSingleResult();
            final Object response = responseBuilder.buildSingleResultQueryResponse(context, getQueryParameters(uriInfo), queryResults, reportItems, uriInfo);
            if (response != null && response instanceof SingleResultQuery) {
                final SingleResultQuery singleResultQuery = (SingleResultQuery) response;
                final List<JAXBElement<?>> item = singleResultQuery.getFields();
                if ((item != null) && (item.size() == 1)) {
                    // Fix for Bug 393320 - JPA-RS: Respect the Accept Header for a singleResultQuery
                    // If there is only one item in the select clause and if value of that item is binary, we will create a response with
                    // that binary data without converting its to Base64.
                    JAXBElement element = item.get(0);
                    Object elementValue = element.getValue();
                    if (elementValue instanceof byte[]) {
                        List<MediaType> acceptableMediaTypes = headers.getAcceptableMediaTypes();
                        if (acceptableMediaTypes.contains(MediaType.APPLICATION_OCTET_STREAM_TYPE)) {
                            return Response.ok(new StreamingOutputMarshaller(context, elementValue, headers.getAcceptableMediaTypes())).build();
                        }
                    }
                }
                return Response.ok(new StreamingOutputMarshaller(context, response, headers.getAcceptableMediaTypes())).build();
            } else {
                // something went wrong with the descriptors, return error
                throw JPARSException.responseCouldNotBeBuiltForNamedQueryRequest(queryName, context.getName());
            }
        }
        final Object queryResult = query.getSingleResult();
        return Response.ok(new StreamingOutputMarshaller(context, responseBuilder.buildSingleEntityResponse(context, getQueryParameters(uriInfo), queryResult, uriInfo), headers.getAcceptableMediaTypes())).build();
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    }
}
Also used : ReportQuery(org.eclipse.persistence.queries.ReportQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Query(jakarta.persistence.Query) SingleResultQuery(org.eclipse.persistence.jpa.rs.util.list.SingleResultQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ReportQuery(org.eclipse.persistence.queries.ReportQuery) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) SingleResultQuery(org.eclipse.persistence.jpa.rs.util.list.SingleResultQuery) JAXBElement(jakarta.xml.bind.JAXBElement) ReportItem(org.eclipse.persistence.internal.queries.ReportItem) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException) FeatureResponseBuilder(org.eclipse.persistence.jpa.rs.features.FeatureResponseBuilder) MediaType(jakarta.ws.rs.core.MediaType) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)

Example 5 with EJBQueryImpl

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

the class CriteriaQueryTestSuite method testOnClauseCompareSQL.

/**
 * This test verifies that the SQL generated for a criteria query with joins and an on clause matches
 * the SQL generated for the equivalent JPQL query, to ensure that excess table joins are not occurring that do
 * not affect the results.
 */
public void testOnClauseCompareSQL() {
    EntityManager em = createEntityManager();
    JpaEntityManager jpaEM = JpaHelper.getEntityManager((EntityManager) em.getDelegate());
    EJBQueryImpl query = (EJBQueryImpl) jpaEM.createQuery("Select e from Employee e left join e.manager m left join m.address a on a.city = 'Ottawa' " + "where a.postalCode is not null");
    String baseSQL = query.getDatabaseQuery().getTranslatedSQLString(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    CriteriaBuilder qb = jpaEM.getCriteriaBuilder();
    CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
    Root<Employee> root = cq.from(Employee.class);
    Join<Object, Object> address = root.join("manager", JoinType.LEFT).join("address", JoinType.LEFT);
    address.on(qb.equal(address.get("city"), "Ottawa"));
    cq.where(qb.isNotNull(address.get("postalCode")));
    EJBQueryImpl testQuery = (EJBQueryImpl) jpaEM.createQuery(cq);
    String testSQL = testQuery.getDatabaseQuery().getTranslatedSQLString(this.getDatabaseSession(), new org.eclipse.persistence.sessions.DatabaseRecord());
    closeEntityManager(em);
    if (!testSQL.equals(baseSQL)) {
        fail("Criteria query using ON clause did not match SQL used for a JPQL query; generated SQL was: \"" + testSQL + "\"  but we expected: \"" + baseSQL + "\"");
    }
}
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) Employee(org.eclipse.persistence.testing.models.jpa21.advanced.Employee)

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