Search in sources :

Example 21 with AbstractSession

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

the class MetadataMirrorFactory method getMetadataProject.

/**
 * INTERNAL:
 * We preserve state from each processor run by holding static references
 * to projects.
 */
public MetadataProject getMetadataProject(SEPersistenceUnitInfo puInfo) {
    if (!metadataProjects.containsKey(puInfo.getPersistenceUnitName())) {
        AbstractSession session = new ServerSession(new Project(new DatabaseLogin()));
        session.setSessionLog(getLogger().getSession().getSessionLog());
        MetadataProject project = new MetadataProject(puInfo, session, false, false, false, false, false);
        metadataProjects.put(puInfo.getPersistenceUnitName(), project);
        return project;
    } else {
        return metadataProjects.get(puInfo.getPersistenceUnitName());
    }
}
Also used : MetadataProject(org.eclipse.persistence.internal.jpa.metadata.MetadataProject) Project(org.eclipse.persistence.sessions.Project) DatabaseLogin(org.eclipse.persistence.sessions.DatabaseLogin) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) MetadataProject(org.eclipse.persistence.internal.jpa.metadata.MetadataProject) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 22 with AbstractSession

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

the class AbstractEntityResource method findAttributeInternal.

protected Response findAttributeInternal(String version, String persistenceUnit, String type, String id, String attribute, HttpHeaders headers, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "findAttributeInternal", new Object[] { "GET", version, persistenceUnit, type, id, attribute, uriInfo.getRequestUri().toASCIIString() });
    EntityManager em = null;
    try {
        PersistenceContext context = getPersistenceContext(persistenceUnit, type, uriInfo.getBaseUri(), version, null);
        Object entityId = IdHelper.buildId(context, type, id);
        em = context.getEmf().createEntityManager(getMatrixParameters(uriInfo, persistenceUnit));
        Object entity = em.find(context.getClass(type), entityId, getQueryParameters(uriInfo));
        DatabaseSession serverSession = context.getServerSession();
        ClassDescriptor descriptor = serverSession.getClassDescriptor(context.getClass(type));
        if (descriptor == null) {
            throw JPARSException.classOrClassDescriptorCouldNotBeFoundForEntity(type, persistenceUnit);
        }
        DatabaseMapping attributeMapping = descriptor.getMappingForAttributeName(attribute);
        if ((attributeMapping == null) || (entity == null)) {
            throw JPARSException.databaseMappingCouldNotBeFoundForEntityAttribute(attribute, type, id, persistenceUnit);
        }
        if (!attributeMapping.isCollectionMapping()) {
            Object result = attributeMapping.getRealAttributeValueFromAttribute(attributeMapping.getAttributeValueFromObject(entity), entity, (AbstractSession) serverSession);
            if (result == null) {
                JPARSLogger.error(context.getSessionLog(), "jpars_could_not_find_entity_for_attribute", new Object[] { attribute, type, id, persistenceUnit });
                throw JPARSException.attributeCouldNotBeFoundForEntity(attribute, type, id, persistenceUnit);
            }
            final FeatureResponseBuilder responseBuilder = context.getSupportedFeatureSet().getResponseBuilder(Feature.NO_PAGING);
            return findAttributeResponse(context, attribute, type, id, persistenceUnit, result, getQueryParameters(uriInfo), headers, uriInfo, responseBuilder, null);
        }
        ReadQuery query = (ReadQuery) ((((ForeignReferenceMapping) attributeMapping).getSelectionQuery()).clone());
        if (query == null) {
            throw JPARSException.selectionQueryForAttributeCouldNotBeFoundForEntity(attribute, type, id, persistenceUnit);
        }
        final FeatureSet featureSet = context.getSupportedFeatureSet();
        final AbstractSession clientSession = context.getClientSession(em);
        // Fields filtering
        FieldsFilter fieldsFilter = null;
        if (context.getSupportedFeatureSet().isSupported(Feature.FIELDS_FILTERING)) {
            final FieldsFilteringValidator fieldsFilteringValidator = new FieldsFilteringValidator(uriInfo);
            if (fieldsFilteringValidator.isFeatureApplicable()) {
                fieldsFilter = fieldsFilteringValidator.getFilter();
            }
        }
        // Pagination
        if (featureSet.isSupported(Feature.PAGING)) {
            final PageableFieldValidator validator = new PageableFieldValidator(entity.getClass(), attribute, uriInfo);
            if (validator.isFeatureApplicable()) {
                // Adding extra one to detect are there more rows or not. It will be removed later
                // on in response processor.
                query.setMaxRows(validator.getLimit() + validator.getOffset() + 1);
                query.setFirstResult(validator.getOffset());
                // We need to add limit and offset to query parameters because request builder reads it from there
                final Map<String, Object> queryParams = getQueryParameters(uriInfo);
                queryParams.put(QueryParameters.JPARS_PAGING_LIMIT, String.valueOf(validator.getLimit()));
                queryParams.put(QueryParameters.JPARS_PAGING_OFFSET, String.valueOf(validator.getOffset()));
                // check orderBy, and generate a warning if there is none
                checkOrderBy(context, query);
                final Object result = clientSession.executeQuery(query, descriptor.getObjectBuilder().buildRow(entity, clientSession, WriteType.INSERT));
                final FeatureResponseBuilder responseBuilder = context.getSupportedFeatureSet().getResponseBuilder(Feature.PAGING);
                return findAttributeResponse(context, attribute, type, id, persistenceUnit, result, queryParams, headers, uriInfo, responseBuilder, fieldsFilter);
            }
        }
        final Object result = clientSession.executeQuery(query, descriptor.getObjectBuilder().buildRow(entity, clientSession, WriteType.INSERT));
        final FeatureResponseBuilder responseBuilder = context.getSupportedFeatureSet().getResponseBuilder(Feature.NO_PAGING);
        return findAttributeResponse(context, attribute, type, id, persistenceUnit, result, getQueryParameters(uriInfo), headers, uriInfo, responseBuilder, fieldsFilter);
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    } finally {
        if (em != null) {
            if (em.isOpen()) {
                em.close();
            }
        }
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) FieldsFilteringValidator(org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilteringValidator) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException) PageableFieldValidator(org.eclipse.persistence.jpa.rs.features.paging.PageableFieldValidator) FeatureResponseBuilder(org.eclipse.persistence.jpa.rs.features.FeatureResponseBuilder) EntityManager(jakarta.persistence.EntityManager) ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) FieldsFilter(org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilter) FeatureSet(org.eclipse.persistence.jpa.rs.features.FeatureSet) ReadQuery(org.eclipse.persistence.queries.ReadQuery) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 23 with AbstractSession

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

the class JTATransactionWrapper method beginTransaction.

@Override
public void beginTransaction(EntityManager em) {
    AbstractSession session = JpaHelper.getEntityManagerFactory(em).getDatabaseSession();
    session.getExternalTransactionController().beginTransaction(session);
    // EMs obtained outside the transaction is now prevented from participating or flushing to the trans unless join is called.
    if (!em.isJoinedToTransaction()) {
        em.joinTransaction();
    }
}
Also used : AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 24 with AbstractSession

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

the class JTATransactionWrapper method commitTransaction.

@Override
public void commitTransaction(EntityManager em) {
    AbstractSession session = JpaHelper.getEntityManagerFactory(em).getDatabaseSession();
    session.getExternalTransactionController().commitTransaction(session);
}
Also used : AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 25 with AbstractSession

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

the class SDOXMLHelperDelegate method initializeDescriptor.

@Override
public void initializeDescriptor(XMLDescriptor descriptor) {
    AbstractSession theSession = (AbstractSession) getXmlContext().getSession();
    // do initialization for new descriptor;
    descriptor.preInitialize(theSession);
    descriptor.initialize(theSession);
    descriptor.postInitialize(theSession);
    descriptor.getObjectBuilder().initializePrimaryKey(theSession);
    getXmlContext().storeXMLDescriptorByQName(descriptor);
}
Also used : 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