Search in sources :

Example 11 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery 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 12 with DatabaseQuery

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

the class EntityManagerImpl method createDescriptorNamedQuery.

@Override
public jakarta.persistence.Query createDescriptorNamedQuery(String queryName, Class<?> descriptorClass, List argumentTypes) {
    try {
        verifyOpen();
        ClassDescriptor descriptor = this.databaseSession.getDescriptor(descriptorClass);
        if (descriptor != null) {
            DatabaseQuery query = descriptor.getQueryManager().getLocalQueryByArgumentTypes(queryName, argumentTypes);
            if (query != null) {
                return new EJBQueryImpl(query, this);
            }
        }
        return null;
    } catch (RuntimeException e) {
        setRollbackOnly();
        throw e;
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery)

Example 13 with DatabaseQuery

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

the class EntityManagerImpl method createQuery.

/**
 * This method is used to create a query using a EclipseLink Expression and
 * the return type.
 */
@Override
public jakarta.persistence.Query createQuery(Expression expression, Class<?> resultType) {
    try {
        verifyOpen();
        DatabaseQuery query = createQueryInternal(expression, resultType);
        return new EJBQueryImpl(query, this);
    } catch (RuntimeException e) {
        setRollbackOnly();
        throw e;
    }
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery)

Example 14 with DatabaseQuery

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

the class EntityManagerFactoryImpl method addNamedQuery.

@Override
public void addNamedQuery(String name, Query query) {
    QueryImpl queryImpl = query.unwrap(QueryImpl.class);
    DatabaseQuery unwrapped = (DatabaseQuery) queryImpl.getDatabaseQueryInternal().clone();
    if (queryImpl.lockMode != null) {
        ((ObjectLevelReadQuery) unwrapped).setLockModeType(queryImpl.lockMode.name(), getServerSession());
    }
    if (unwrapped.isReadQuery()) {
        ((ReadQuery) unwrapped).setInternalMax((queryImpl.getMaxResultsInternal()));
        ((ReadQuery) unwrapped).setFirstResult((queryImpl.getFirstResult()));
    }
    this.getServerSession().addQuery(name, unwrapped, true);
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) ReadQuery(org.eclipse.persistence.queries.ReadQuery)

Example 15 with DatabaseQuery

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

the class AbstractPersistenceUnitResource method getQueryMetadataInternal.

protected Response getQueryMetadataInternal(String version, String persistenceUnit, String queryName, HttpHeaders headers, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "getQueryMetadataInternal", new Object[] { "GET", version, persistenceUnit, queryName, uriInfo.getRequestUri().toASCIIString() });
    try {
        PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
        List<Query> returnQueries = new ArrayList<>();
        Map<String, List<DatabaseQuery>> queries = context.getServerSession().getQueries();
        if (queries.get(queryName) != null) {
            for (DatabaseQuery query : queries.get(queryName)) {
                returnQueries.add(getQuery(query, context));
            }
        }
        String mediaType = StreamingOutputMarshaller.mediaType(headers.getAcceptableMediaTypes()).toString();
        QueryList queryList = new QueryList();
        queryList.setList(returnQueries);
        String result = null;
        if (mediaType.equals(MediaType.APPLICATION_JSON)) {
            result = marshallMetadata(queryList.getList(), mediaType);
        } else {
            result = marshallMetadata(queryList, mediaType);
        }
        return Response.ok(new StreamingOutputMarshaller(null, result, headers.getAcceptableMediaTypes())).build();
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    }
}
Also used : ReportQuery(org.eclipse.persistence.queries.ReportQuery) Query(org.eclipse.persistence.internal.jpa.rs.metadata.model.Query) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ArrayList(java.util.ArrayList) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) ArrayList(java.util.ArrayList) QueryList(org.eclipse.persistence.jpa.rs.util.list.QueryList) List(java.util.List) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller) QueryList(org.eclipse.persistence.jpa.rs.util.list.QueryList) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException)

Aggregations

DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)86 ArrayList (java.util.ArrayList)18 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)15 List (java.util.List)14 Vector (java.util.Vector)12 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)12 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)12 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)8 EntityManager (jakarta.persistence.EntityManager)6 HashMap (java.util.HashMap)6 QName (javax.xml.namespace.QName)6 EJBQueryImpl (org.eclipse.persistence.internal.jpa.EJBQueryImpl)6 PersistenceContext (org.eclipse.persistence.jpa.rs.PersistenceContext)6 DataReadQuery (org.eclipse.persistence.queries.DataReadQuery)6 Session (org.eclipse.persistence.sessions.Session)6 Test (org.junit.Test)6 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)5 ReadQuery (org.eclipse.persistence.queries.ReadQuery)5 ReportQuery (org.eclipse.persistence.queries.ReportQuery)5 PersistenceException (jakarta.persistence.PersistenceException)4