Search in sources :

Example 11 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.

the class EntityManagerImpl method getQueryHints.

/**
 * Before any find or refresh operation, gather any persistence unit
 * properties that should be applied to the query.
 */
protected HashMap<String, Object> getQueryHints(Object entity, OperationType operation) {
    HashMap<String, Object> queryHints = null;
    // could likely do it here as well.
    if (entity != null && properties != null) {
        queryHints = new HashMap<String, Object>();
        if (properties.containsKey(QueryHints.PESSIMISTIC_LOCK_TIMEOUT)) {
            queryHints.put(QueryHints.PESSIMISTIC_LOCK_TIMEOUT, properties.get(QueryHints.PESSIMISTIC_LOCK_TIMEOUT));
        }
        if (properties.containsKey(QueryHints.PESSIMISTIC_LOCK_TIMEOUT_UNIT)) {
            queryHints.put(QueryHints.PESSIMISTIC_LOCK_TIMEOUT_UNIT, properties.get(QueryHints.PESSIMISTIC_LOCK_TIMEOUT_UNIT));
        }
        // been specified.
        if (!properties.containsKey(QueryHints.CACHE_USAGE)) {
            // If the descriptor is isolated then it is not cacheable so ignore
            // the properties. A null descriptor case will be handled in the
            // individual operation methods so no need to worry about it here.
            Class<?> cls = entity instanceof Class<?> ? (Class) entity : entity.getClass();
            ClassDescriptor descriptor = getActiveSession().getDescriptor(cls);
            if (descriptor != null && !descriptor.isIsolated()) {
                if (operation != OperationType.LOCK) {
                    // For a find operation, apply the jakarta.persistence.cache.retrieveMode
                    if (operation == OperationType.FIND) {
                        if (properties.containsKey(QueryHints.CACHE_RETRIEVE_MODE)) {
                            queryHints.put(QueryHints.CACHE_RETRIEVE_MODE, properties.get(QueryHints.CACHE_RETRIEVE_MODE));
                        } else if (properties.containsKey("jakarta.persistence.cacheRetrieveMode")) {
                            // support legacy property
                            Session activeSession = getActiveSession();
                            if (activeSession != null) {
                                // log deprecation info
                                String[] properties = new String[] { QueryHints.CACHE_RETRIEVE_MODE, "jakarta.persistence.cacheRetrieveMode" };
                                ((AbstractSession) activeSession).log(SessionLog.INFO, SessionLog.TRANSACTION, "deprecated_property", properties);
                            }
                            queryHints.put(QueryHints.CACHE_RETRIEVE_MODE, properties.get("jakarta.persistence.cacheRetrieveMode"));
                        }
                    }
                    // For both find and refresh operations, apply jakarta.persistence.cache.storeMode
                    if (properties.containsKey(QueryHints.CACHE_STORE_MODE)) {
                        queryHints.put(QueryHints.CACHE_STORE_MODE, properties.get(QueryHints.CACHE_STORE_MODE));
                    } else if (properties.containsKey("jakarta.persistence.cacheStoreMode")) {
                        // support legacy property
                        Session activeSession = getActiveSession();
                        if (activeSession != null) {
                            // log deprecation info
                            String[] properties = new String[] { QueryHints.CACHE_STORE_MODE, "jakarta.persistence.cacheStoreMode" };
                            ((AbstractSession) activeSession).log(SessionLog.INFO, SessionLog.TRANSACTION, "deprecated_property", properties);
                        }
                        queryHints.put(QueryHints.CACHE_STORE_MODE, properties.get("jakarta.persistence.cacheStoreMode"));
                    }
                }
            }
        }
    }
    return queryHints;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session)

Example 12 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.

the class EntityManagerImpl method find.

/**
 * Find by primary key.
 *
 * @param entityName
 *            - the entity class to find.
 * @param primaryKey
 *            - the entity primary key value, or primary key class, or a
 *            List of primary key values.
 * @return the found entity instance or null, if the entity does not exist.
 * @throws IllegalArgumentException
 *             if the first argument does not indicate an entity or if the
 *             second argument is not a valid type for that entity's
 *             primaryKey.
 */
public Object find(String entityName, Object primaryKey) {
    try {
        verifyOpen();
        AbstractSession session = (AbstractSession) getActiveSession();
        ClassDescriptor descriptor = session.getDescriptorForAlias(entityName);
        if (descriptor == null || descriptor.isDescriptorTypeAggregate()) {
            throw new IllegalArgumentException(ExceptionLocalization.buildMessage("unknown_entitybean_name", new Object[] { entityName }));
        }
        return findInternal(descriptor, session, primaryKey, null, null);
    } catch (LockTimeoutException e) {
        throw e;
    } catch (RuntimeException e) {
        setRollbackOnly();
        throw e;
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) LockTimeoutException(jakarta.persistence.LockTimeoutException) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 13 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.

the class EntityManagerImpl method getActivePersistenceContext.

public RepeatableWriteUnitOfWork getActivePersistenceContext(Object txn) {
    // use local uow as it will be local to this EM and not on the txn
    if (this.extendedPersistenceContext == null || !this.extendedPersistenceContext.isActive()) {
        AbstractSession client;
        if (this.databaseSession.isServerSession()) {
            createConnectionPolicy();
            client = ((ServerSession) this.databaseSession).acquireClientSession(this.connectionPolicy, this.properties);
        } else if (this.databaseSession.isBroker()) {
            Map mapOfProperties = null;
            if (properties != null) {
                mapOfProperties = (Map) this.properties.get(EntityManagerProperties.COMPOSITE_UNIT_PROPERTIES);
            }
            createConnectionPolicies(mapOfProperties);
            client = ((SessionBroker) this.databaseSession).acquireClientSessionBroker(this.connectionPolicies, mapOfProperties);
        } else {
            // currently this can't happen - the databaseSession is either ServerSession or SessionBroker.
            client = this.databaseSession;
        }
        this.extendedPersistenceContext = client.acquireRepeatableWriteUnitOfWork(this.referenceMode);
        this.extendedPersistenceContext.setResumeUnitOfWorkOnTransactionCompletion(!this.closeOnCommit);
        this.extendedPersistenceContext.setShouldDiscoverNewObjects(this.persistOnCommit);
        this.extendedPersistenceContext.setDiscoverUnregisteredNewObjectsWithoutPersist(this.commitWithoutPersistRules);
        this.extendedPersistenceContext.setFlushClearCache(this.flushClearCache);
        this.extendedPersistenceContext.setShouldValidateExistence(this.shouldValidateExistence);
        this.extendedPersistenceContext.setCommitOrder(this.commitOrder);
        this.extendedPersistenceContext.setShouldCascadeCloneToJoinedRelationship(true);
        this.extendedPersistenceContext.setShouldStoreByPassCache(this.cacheStoreBypass);
        if (txn != null) {
            // if there is a txn, it means we have been marked to join with it.
            // All that is left is to register the UOW with the transaction
            transaction.registerIfRequired(this.extendedPersistenceContext);
        }
        if (client.shouldLog(SessionLog.FINER, SessionLog.TRANSACTION)) {
            client.log(SessionLog.FINER, SessionLog.TRANSACTION, "acquire_unit_of_work_with_argument", String.valueOf(System.identityHashCode(this.extendedPersistenceContext)));
        }
    }
    if (this.beginEarlyTransaction && txn != null && !this.extendedPersistenceContext.isInTransaction()) {
        // gf3334, force persistence context early transaction
        this.extendedPersistenceContext.beginEarlyTransaction();
    }
    return this.extendedPersistenceContext;
}
Also used : SessionBroker(org.eclipse.persistence.sessions.broker.SessionBroker) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 14 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.

the class EntityManagerFactoryDelegate method createEntityManagerImpl.

protected EntityManagerImpl createEntityManagerImpl(Map properties, SynchronizationType syncType) {
    verifyOpen();
    AbstractSession session = getAbstractSession();
    if (session.isDatabaseSession()) {
        DatabaseSessionImpl databaseSession = (DatabaseSessionImpl) session;
        if (!databaseSession.isLoggedIn()) {
            // PERF: Avoid synchronization.
            synchronized (databaseSession) {
                // complete.
                if (!databaseSession.isLoggedIn()) {
                    databaseSession.login();
                }
            }
        }
    }
    if (syncType != null && !session.hasExternalTransactionController()) {
        throw new IllegalStateException(ExceptionLocalization.buildMessage("pu_configured_for_resource_local"));
    }
    return new EntityManagerImpl(this, properties, syncType);
}
Also used : DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 15 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.

the class RestCollectionAdapter method loadItems.

private Collection loadItems(String href) throws Exception {
    String uri = href.replace("\\/", "/");
    uri = uri.substring(uri.indexOf("entity/"));
    uri = uri.substring(uri.indexOf('/') + 1);
    final String[] uriItems = uri.split("/");
    final String entityType = uriItems[0];
    final String entityId = uriItems[1];
    final String attributeName = uriItems[2];
    final ClassDescriptor descriptor = context.getDescriptor(entityType);
    final Object id = IdHelper.buildId(context, descriptor.getAlias(), entityId);
    final T entity = getObjectById(entityType, id);
    final DatabaseMapping attributeMapping = descriptor.getMappingForAttributeName(attributeName);
    if ((attributeMapping == null) || (entity == null)) {
        throw JPARSException.databaseMappingCouldNotBeFoundForEntityAttribute(attributeName, entityType, entityId, null);
    }
    return (Collection) attributeMapping.getRealAttributeValueFromAttribute(attributeMapping.getAttributeValueFromObject(entity), entity, (AbstractSession) context.getServerSession());
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Collection(java.util.Collection) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)215 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)52 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)28 ArrayList (java.util.ArrayList)26 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)26 Vector (java.util.Vector)22 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)22 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)21 EntityManager (jakarta.persistence.EntityManager)19 List (java.util.List)18 Session (org.eclipse.persistence.sessions.Session)18 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)15 InvalidObject (org.eclipse.persistence.internal.helper.InvalidObject)14 UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)14 HashMap (java.util.HashMap)12 Map (java.util.Map)12 Collection (java.util.Collection)11 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)11 SQLException (java.sql.SQLException)10 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)9