Search in sources :

Example 1 with ObjectLevelReadQuery

use of org.eclipse.persistence.queries.ObjectLevelReadQuery 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 ObjectLevelReadQuery

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

the class QueryImpl method executeReadQuery.

/**
 * Execute a ReadQuery by assigning the stored parameter values and running
 * it in the database
 *
 * @return the results of the query execution
 */
protected Object executeReadQuery() {
    List<Object> parameterValues = processParameters();
    // TODO: the following performFlush() call is a temporary workaround for
    // bug 4752493:
    // CTS: INMEMORY QUERYING IN EJBQUERY BROKEN DUE TO CHANGE TO USE
    // REPORTQUERY.
    // Ideally we should only flush in case the selectionExpression can't be
    // conformed in memory.
    // There are two alternative ways to implement that:
    // 1. Try running the query with conformInUOW flag first - if it fails
    // with
    // QueryException.cannotConformExpression then flush and run the query
    // again -
    // now without conforming.
    // 2. Implement a new isComformable method on Expression which would
    // determine whether the expression
    // could be conformed in memory, flush only in case it returns false.
    // Note that doesConform method currently implemented on Expression
    // requires object(s) to be confirmed as parameter(s).
    // The new isComformable method should not take any objects as
    // parameters,
    // it should return false if there could be such an object that
    // passed to doesConform causes it to throw
    // QueryException.cannotConformExpression -
    // and true otherwise.
    boolean shouldResetConformResultsInUnitOfWork = false;
    DatabaseQuery query = getDatabaseQueryInternal();
    boolean isObjectLevelReadQuery = query.isObjectLevelReadQuery();
    if (isFlushModeAUTO() && (!isObjectLevelReadQuery || !((ObjectLevelReadQuery) query).isReadOnly())) {
        performPreQueryFlush();
        if (isObjectLevelReadQuery) {
            if (((ObjectLevelReadQuery) query).shouldConformResultsInUnitOfWork()) {
                cloneSharedQuery();
                query = getDatabaseQueryInternal();
                ((ObjectLevelReadQuery) query).setCacheUsage(ObjectLevelReadQuery.UseDescriptorSetting);
                shouldResetConformResultsInUnitOfWork = true;
            }
        }
    }
    // Set a pessimistic locking on the query if specified.
    if (this.lockMode != null && !this.lockMode.equals(LockModeType.NONE)) {
        // We need to throw TransactionRequiredException if there is no
        // active transaction
        this.entityManager.checkForTransaction(true);
        // The lock mode setters and getters validate the query type
        // so should be safe to make the casting.
        cloneSharedQuery();
        query = getDatabaseQueryInternal();
        // we were unable to set the lock mode.
        if (((ObjectLevelReadQuery) query).setLockModeType(lockMode.name(), (AbstractSession) getActiveSession())) {
            throw new PersistenceException(ExceptionLocalization.buildMessage("ejb30-wrong-lock_called_without_version_locking-index", null));
        }
    }
    Session session = getActiveSession();
    try {
        // in case it's a user-defined query
        if (query.isUserDefined()) {
            // and there is an active transaction
            if (this.entityManager.checkForTransaction(false) != null) {
                // verify whether uow has begun early transaction
                if (session.isUnitOfWork() && !((UnitOfWorkImpl) session).wasTransactionBegunPrematurely()) {
                    // uow begins early transaction in case it hasn't
                    // already begun.
                    // TODO: This is not good, it means that no SQL queries
                    // can ever use the cache,
                    // using isUserDefined to mean an SQL query is also
                    // wrong.
                    ((UnitOfWorkImpl) session).beginEarlyTransaction();
                }
            }
        }
        // Execute the query and return the result.
        return session.executeQuery(query, parameterValues);
    } catch (DatabaseException e) {
        throw getDetailedException(e);
    } catch (RuntimeException e) {
        setRollbackOnly();
        throw e;
    } finally {
        this.lockMode = null;
        if (shouldResetConformResultsInUnitOfWork) {
            ((ObjectLevelReadQuery) query).conformResultsInUnitOfWork();
        }
    }
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) PersistenceException(jakarta.persistence.PersistenceException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session)

Example 3 with ObjectLevelReadQuery

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

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

the class JPQLParseTree method populateQuery.

/**
 * Add all of the relevant query settings from an EJBQLParseTree to the given
 * database query.
 * @param query The query to populate
 * @param session The session to use to information such as descriptors.
 */
public void populateQuery(DatabaseQuery query, AbstractSession session) {
    if (query.isObjectLevelReadQuery()) {
        ObjectLevelReadQuery objectQuery = (ObjectLevelReadQuery) query;
        GenerationContext generationContext = buildContext(objectQuery, session);
        populateReadQueryInternal(objectQuery, generationContext);
    } else if (query.isUpdateAllQuery()) {
        UpdateAllQuery updateQuery = (UpdateAllQuery) query;
        GenerationContext generationContext = buildContext(updateQuery, session);
        populateModifyQueryInternal(updateQuery, generationContext);
        addUpdatesToQuery(updateQuery, generationContext);
    } else if (query.isDeleteAllQuery()) {
        DeleteAllQuery deleteQuery = (DeleteAllQuery) query;
        GenerationContext generationContext = buildContext(deleteQuery, session);
        populateModifyQueryInternal(deleteQuery, generationContext);
    }
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DeleteAllQuery(org.eclipse.persistence.queries.DeleteAllQuery) UpdateAllQuery(org.eclipse.persistence.queries.UpdateAllQuery)

Example 5 with ObjectLevelReadQuery

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

the class IndirectionPolicy method mergeClientIntoServerValueHolder.

/**
 * INTERNAL
 * Replace the client value holder with the server value holder,
 * after copying some of the settings from the client value holder.
 */
protected void mergeClientIntoServerValueHolder(RemoteValueHolder<?> serverValueHolder, MergeManager mergeManager) {
    serverValueHolder.setMapping(this.mapping);
    serverValueHolder.setSession(mergeManager.getSession());
    if (this.mapping.isForeignReferenceMapping()) {
        ObjectLevelReadQuery query = buildCascadeQuery(mergeManager);
        serverValueHolder.setQuery(query);
    }
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery)

Aggregations

ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)67 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)15 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)14 Expression (org.eclipse.persistence.expressions.Expression)13 ObjectExpression (org.eclipse.persistence.internal.expressions.ObjectExpression)10 FetchGroup (org.eclipse.persistence.queries.FetchGroup)10 ReadQuery (org.eclipse.persistence.queries.ReadQuery)10 ArrayList (java.util.ArrayList)9 Query (jakarta.persistence.Query)8 EJBQueryImpl (org.eclipse.persistence.internal.jpa.EJBQueryImpl)8 ReportQuery (org.eclipse.persistence.queries.ReportQuery)8 IdentityHashMap (java.util.IdentityHashMap)7 QueryKeyExpression (org.eclipse.persistence.internal.expressions.QueryKeyExpression)7 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)7 HashMap (java.util.HashMap)6 Vector (java.util.Vector)6 SQLSelectStatement (org.eclipse.persistence.internal.expressions.SQLSelectStatement)6 EntityManager (jakarta.persistence.EntityManager)5 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)5 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)5