Search in sources :

Example 6 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession 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 7 with AbstractSession

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

the class PersistenceContext method setRelationshipInfo.

private void setRelationshipInfo(Object entity) {
    if ((entity != null) && (entity instanceof PersistenceWeavedRest)) {
        ClassDescriptor descriptor = getServerSession().getClassDescriptor(entity.getClass());
        if (descriptor != null) {
            ((PersistenceWeavedRest) entity)._persistence_setRelationships(new ArrayList<>());
            for (DatabaseMapping mapping : descriptor.getMappings()) {
                if (mapping.isForeignReferenceMapping()) {
                    ForeignReferenceMapping frMapping = (ForeignReferenceMapping) mapping;
                    RelationshipInfo info = new RelationshipInfo();
                    info.setAttributeName(frMapping.getAttributeName());
                    info.setOwningEntity(entity);
                    info.setOwningEntityAlias(descriptor.getAlias());
                    info.setPersistencePrimaryKey(descriptor.getObjectBuilder().extractPrimaryKeyFromObject(entity, (AbstractSession) getServerSession()));
                    ((PersistenceWeavedRest) entity)._persistence_getRelationships().add(info);
                } else if (mapping.isEISMapping()) {
                    if (mapping instanceof EISCompositeCollectionMapping) {
                        EISCompositeCollectionMapping eisMapping = (EISCompositeCollectionMapping) mapping;
                        RelationshipInfo info = new RelationshipInfo();
                        info.setAttributeName(eisMapping.getAttributeName());
                        info.setOwningEntity(entity);
                        info.setOwningEntityAlias(descriptor.getAlias());
                        info.setPersistencePrimaryKey(descriptor.getObjectBuilder().extractPrimaryKeyFromObject(entity, (AbstractSession) getServerSession()));
                        ((PersistenceWeavedRest) entity)._persistence_getRelationships().add(info);
                    }
                }
            }
        }
    }
}
Also used : ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) RelationshipInfo(org.eclipse.persistence.internal.jpa.rs.weaving.RelationshipInfo) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) PersistenceWeavedRest(org.eclipse.persistence.internal.jpa.rs.weaving.PersistenceWeavedRest) EISCompositeCollectionMapping(org.eclipse.persistence.eis.mappings.EISCompositeCollectionMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 8 with AbstractSession

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

the class TransformerFactory method addClassDetailsForMappedSuperClasses.

/**
 * INTERNAL:
 * Look higher in the hierarchy for the mappings listed in the unMappedAttribute list.
 *
 * We assume that if a mapping exists, the attribute must either be mapped from the owning
 * class or from a superclass.
 */
public void addClassDetailsForMappedSuperClasses(MetadataClass clz, ClassDescriptor initialDescriptor, ClassDetails classDetails, Map<String, ClassDetails> classDetailsMap, List<DatabaseMapping> unMappedAttributes, boolean weaveChangeTracking) {
    MetadataClass superClz = clz.getSuperclass();
    if (superClz == null || superClz.isObject()) {
        return;
    }
    ClassDescriptor mappedSuperclassDescriptor = ((AbstractSession) session).getMappedSuperclass(superClz.getName());
    if (mappedSuperclassDescriptor == null) {
        ClassDescriptor descriptor = findDescriptor(session.getProject(), clz.getSuperclass().getName());
        if (descriptor != null) {
            return;
        }
    }
    // If the superclass declares more mappings than there are unmapped mappings still to process, this superclass has shadowed attributes
    if (mappedSuperclassDescriptor != null && unMappedAttributes.size() < mappedSuperclassDescriptor.getMappings().size()) {
        List<DatabaseMapping> hiddenMappings = new ArrayList<DatabaseMapping>();
        for (DatabaseMapping mapping : mappedSuperclassDescriptor.getMappings()) {
            boolean contains = false;
            String name = mapping.getAttributeName();
            for (DatabaseMapping newmapping : unMappedAttributes) {
                if (name.equals(newmapping.getAttributeName())) {
                    contains = true;
                    break;
                }
            }
            // If the super has a mapping that wasn't processed by the subclasses, add it to be processed
            if (!contains) {
                hiddenMappings.add(mapping);
            }
        }
        unMappedAttributes.addAll(hiddenMappings);
    }
    boolean weaveValueHolders = canWeaveValueHolders(superClz, unMappedAttributes);
    List<DatabaseMapping> stillUnMappedMappings = null;
    ClassDetails superClassDetails = createClassDetails(superClz, weaveValueHolders, weaveChangeTracking, weaveFetchGroups, weaveInternal, weaveRest);
    superClassDetails.setIsMappedSuperClass(true);
    if (mappedSuperclassDescriptor != null && !mappedSuperclassDescriptor.usesPropertyAccessForWeaving()) {
        superClassDetails.useAttributeAccess();
    } else if (!initialDescriptor.usesPropertyAccessForWeaving()) {
        superClassDetails.useAttributeAccess();
    }
    if (!classDetailsMap.containsKey(superClassDetails.getClassName())) {
        stillUnMappedMappings = storeAttributeMappings(superClz, superClassDetails, unMappedAttributes, weaveValueHolders);
        classDetailsMap.put(superClassDetails.getClassName(), superClassDetails);
        addClassDetailsForMappedSuperClasses(superClz, initialDescriptor, classDetails, classDetailsMap, stillUnMappedMappings, weaveChangeTracking);
    }
}
Also used : MetadataClass(org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataClass) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) ArrayList(java.util.ArrayList) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 9 with AbstractSession

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

the class EntityManagerSetupImpl method addSessionToGlobalSessionManager.

/**
 * Put the given session into the session manager so it can be looked up later
 */
protected void addSessionToGlobalSessionManager() {
    SessionManager sm = SessionManager.getManager();
    ConcurrentMap<String, Session> sessions = sm.getSessions();
    AbstractSession oldSession = (AbstractSession) sessions.get(session.getName());
    if (oldSession != null) {
        throw new PersistenceException(EntityManagerSetupException.attemptedRedeployWithoutClose(session.getName()));
    }
    sm.addSession(session);
}
Also used : RMIServerSessionManager(org.eclipse.persistence.sessions.remote.rmi.RMIServerSessionManager) SessionManager(org.eclipse.persistence.sessions.factories.SessionManager) PersistenceException(jakarta.persistence.PersistenceException) EntityManagerFactoryProvider.getConfigPropertyAsString(org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.getConfigPropertyAsString) Session(org.eclipse.persistence.sessions.Session) RemoteSession(org.eclipse.persistence.sessions.remote.RemoteSession) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 10 with AbstractSession

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

the class EntityManagerSetupImpl method deployCompositeMembers.

protected void deployCompositeMembers(Map deployProperties, ClassLoader realClassLoader) {
    // Don't log these properties - may contain passwords. The properties will be logged by contained persistence units.
    Map compositeMemberMapOfProperties = (Map) getConfigProperty(PersistenceUnitProperties.COMPOSITE_UNIT_PROPERTIES, deployProperties);
    for (EntityManagerSetupImpl compositeMemberEmSetupImpl : this.compositeMemberEmSetupImpls) {
        // the properties guaranteed to be non-null after updateCompositeMemberProperties call
        Map compositeMemberProperties = (Map) compositeMemberMapOfProperties.get(compositeMemberEmSetupImpl.getPersistenceUnitInfo().getPersistenceUnitName());
        compositeMemberEmSetupImpl.deploy(realClassLoader, compositeMemberProperties);
        AbstractSession containedSession = compositeMemberEmSetupImpl.getSession();
        ((SessionBroker) session).registerSession(containedSession.getName(), containedSession);
    }
}
Also used : SessionBroker(org.eclipse.persistence.sessions.broker.SessionBroker) Map(java.util.Map) ConcurrentMap(java.util.concurrent.ConcurrentMap) IdentityMap(org.eclipse.persistence.internal.identitymaps.IdentityMap) HashMap(java.util.HashMap) 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