Search in sources :

Example 36 with DatabaseQuery

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

the class DatasourceCallQueryMechanism method prepareCall.

/**
 * INTERNAL:
 * This is different from 'prepareForExecution' in that this is called on the original query,
 * and the other is called on the copy of the query.
 * This query is copied for concurrency so this prepare can only setup things that
 * will apply to any future execution of this query.
 */
public void prepareCall() throws QueryException {
    DatabaseQuery query = getQuery();
    AbstractSession executionSession = query.getExecutionSession();
    if (hasMultipleCalls()) {
        for (DatasourceCall call : (List<DatasourceCall>) getCalls()) {
            call.prepare(executionSession);
        }
    } else if (getCall() != null) {
        getCall().prepare(executionSession);
    }
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) List(java.util.List) DatasourceCall(org.eclipse.persistence.internal.databaseaccess.DatasourceCall) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 37 with DatabaseQuery

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

the class TestTimestampVersionLocking method testLocalTimestampProperty.

/**
 * Check that setting the property "true" will get the local system time
 * instead of the default behavior of contacting the server.
 */
@Test
public void testLocalTimestampProperty() throws Exception {
    EntityManager em = emf.createEntityManager();
    try {
        em.getTransaction().begin();
        // Get the session for this transaction
        Session session = ((EntityManagerImpl) em).getActiveSession();
        // Get the TimestampQuery that would be used by the platform to get the current time
        DatabaseQuery query = session.getDatasourcePlatform().getTimestampQuery();
        // Add a Listener to the session
        QueryListener listener = new QueryListener(query);
        session.getEventManager().addListener(listener);
        // Persist an Entity that will use Timestamp version locking and will trigger QueryListener
        TimestampDog dog = new TimestampDog();
        em.persist(dog);
        em.getTransaction().commit();
        // Make sure the query was not executed
        Assert.assertTrue("Query (" + listener.getQuery().getSQLString() + ") was executed unexpectedly.", !listener.wasQueryExecuted());
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) TimestampDog(org.eclipse.persistence.jpa.test.locking.model.TimestampDog) Session(org.eclipse.persistence.sessions.Session) Test(org.junit.Test)

Example 38 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project cuba by cuba-platform.

the class QueryImpl method executeUpdate.

@Override
public int executeUpdate() {
    JpaQuery<T> jpaQuery = getQuery();
    DatabaseQuery databaseQuery = jpaQuery.getDatabaseQuery();
    Class referenceClass = databaseQuery.getReferenceClass();
    boolean isDeleteQuery = databaseQuery.isDeleteObjectQuery() || databaseQuery.isDeleteAllQuery();
    boolean enableDeleteInSoftDeleteMode = Boolean.parseBoolean(AppContext.getProperty("cuba.enableDeleteStatementInSoftDeleteMode"));
    if (!enableDeleteInSoftDeleteMode && entityManager.isSoftDeletion() && isDeleteQuery) {
        if (SoftDelete.class.isAssignableFrom(referenceClass)) {
            throw new UnsupportedOperationException("Delete queries are not supported with enabled soft deletion. " + "Use 'cuba.enableDeleteStatementInSoftDeleteMode' application property to roll back to legacy behavior.");
        }
    }
    // In some cache configurations (in particular, when shared cache is on, but for some entities cache is set to ISOLATED),
    // EclipseLink does not evict updated entities from cache automatically.
    Cache cache = jpaQuery.getEntityManager().getEntityManagerFactory().getCache();
    if (referenceClass != null) {
        cache.evict(referenceClass);
        queryCacheMgr.invalidate(referenceClass, true);
    } else {
        cache.evictAll();
        queryCacheMgr.invalidateAll(true);
    }
    preExecute(jpaQuery);
    return jpaQuery.executeUpdate();
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) MetaClass(com.haulmont.chile.core.model.MetaClass) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass)

Example 39 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project jmix by jmix-framework.

the class JmixEclipseLinkQuery method preExecute.

private void preExecute(JpaQuery<E> 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
        support.processFlush(entityManager, true);
        entityChangedEventManager.beforeFlush(support.getInstances(entityManager));
    }
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl)

Example 40 with DatabaseQuery

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

the class XRServiceFactory method updateFindQueryNames.

/**
 * <p>INTERNAL:
 *
 * Legacy projects have 'findAll' and 'findByPrimaryKey' queries, whereas we now
 * expect these to have the descriptor alias appended (preceded with underscore),
 * + 'Type'.  For example, if we have an Employee descriptor, the find queries
 * would be:  'findAll_employeeType' and 'findByPrimaryKey_employeeType'.
 */
@SuppressWarnings("rawtypes")
protected static void updateFindQueryNames(Project orProject) {
    for (ClassDescriptor orDesc : orProject.getDescriptors().values()) {
        Vector queries = orDesc.getQueryManager().getAllQueries();
        for (int i = 0; i < queries.size(); i++) {
            DatabaseQuery query = (DatabaseQuery) queries.get(i);
            String qName = query.getName();
            String END_PART = UNDERSCORE_STR + query.getDescriptor().getAlias() + TYPE_STR;
            if ((PK_QUERYNAME.equals(qName) || ALL_QUERYNAME.equals(qName)) && !qName.endsWith(END_PART)) {
                orDesc.getQueryManager().addQuery(qName + END_PART, query);
            }
        }
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Vector(java.util.Vector)

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