Search in sources :

Example 16 with EntityKey

use of org.hibernate.engine.spi.EntityKey in project hibernate-orm by hibernate.

the class EntityReferenceInitializerImpl method handleMissingIdentifier.

private void handleMissingIdentifier(ResultSetProcessingContext context) {
    if (EntityFetch.class.isInstance(entityReference)) {
        final EntityFetch fetch = (EntityFetch) entityReference;
        final EntityType fetchedType = fetch.getFetchedType();
        if (!fetchedType.isOneToOne()) {
            return;
        }
        final EntityReferenceProcessingState fetchOwnerState = context.getOwnerProcessingState(fetch);
        if (fetchOwnerState == null) {
            throw new IllegalStateException("Could not locate fetch owner state");
        }
        final EntityKey ownerEntityKey = fetchOwnerState.getEntityKey();
        if (ownerEntityKey != null) {
            context.getSession().getPersistenceContext().addNullProperty(ownerEntityKey, fetchedType.getPropertyName());
        }
    }
}
Also used : EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) EntityType(org.hibernate.type.EntityType) EntityKey(org.hibernate.engine.spi.EntityKey) EntityReferenceProcessingState(org.hibernate.loader.plan.exec.process.spi.ResultSetProcessingContext.EntityReferenceProcessingState)

Example 17 with EntityKey

use of org.hibernate.engine.spi.EntityKey in project hibernate-orm by hibernate.

the class EntityReferenceInitializerImpl method hydrateEntityState.

@Override
public void hydrateEntityState(ResultSet resultSet, ResultSetProcessingContextImpl context) {
    final EntityReferenceProcessingState processingState = context.getProcessingState(entityReference);
    // If there is no identifier for this entity reference for this row, nothing to do
    if (processingState.isMissingIdentifier()) {
        handleMissingIdentifier(context);
        return;
    }
    // make sure we have the EntityKey
    final EntityKey entityKey = processingState.getEntityKey();
    if (entityKey == null) {
        handleMissingIdentifier(context);
        return;
    }
    // Have we already hydrated this entity's state?
    if (processingState.getEntityInstance() != null) {
        return;
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // In getting here, we know that:
    // 		1) We need to hydrate the entity state
    //		2) We have a valid EntityKey for the entity
    // see if we have an existing entry in the session for this EntityKey
    final Object existing = context.getSession().getEntityUsingInterceptor(entityKey);
    if (existing != null) {
        // It is previously associated with the Session, perform some checks
        if (!entityReference.getEntityPersister().isInstance(existing)) {
            throw new WrongClassException("loaded object was of wrong class " + existing.getClass(), entityKey.getIdentifier(), entityReference.getEntityPersister().getEntityName());
        }
        checkVersion(resultSet, context, entityKey, existing);
        // use the existing association as the hydrated state
        processingState.registerEntityInstance(existing);
        //context.registerHydratedEntity( entityReference, entityKey, existing );
        return;
    }
    // Otherwise, we need to load it from the ResultSet...
    // determine which entity instance to use.  Either the supplied one, or instantiate one
    Object entityInstance = null;
    if (isReturn && context.shouldUseOptionalEntityInformation() && context.getQueryParameters().getOptionalObject() != null) {
        final EntityKey optionalEntityKey = ResultSetProcessorHelper.getOptionalObjectKey(context.getQueryParameters(), context.getSession());
        if (optionalEntityKey != null && optionalEntityKey.equals(entityKey)) {
            entityInstance = context.getQueryParameters().getOptionalObject();
        }
    }
    final String concreteEntityTypeName = getConcreteEntityTypeName(resultSet, context, entityKey);
    if (entityInstance == null) {
        entityInstance = context.getSession().instantiate(concreteEntityTypeName, entityKey.getIdentifier());
    }
    processingState.registerEntityInstance(entityInstance);
    // need to hydrate it.
    // grab its state from the ResultSet and keep it in the Session
    // (but don't yet initialize the object itself)
    // note that we acquire LockMode.READ even if it was not requested
    log.trace("hydrating entity state");
    final LockMode requestedLockMode = context.resolveLockMode(entityReference);
    final LockMode lockModeToAcquire = requestedLockMode == LockMode.NONE ? LockMode.READ : requestedLockMode;
    loadFromResultSet(resultSet, context, entityInstance, concreteEntityTypeName, entityKey, lockModeToAcquire);
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) EntityReferenceProcessingState(org.hibernate.loader.plan.exec.process.spi.ResultSetProcessingContext.EntityReferenceProcessingState) LockMode(org.hibernate.LockMode) WrongClassException(org.hibernate.WrongClassException)

Example 18 with EntityKey

use of org.hibernate.engine.spi.EntityKey in project hibernate-orm by hibernate.

the class ResultSetProcessingContextImpl method createSubselects.

private void createSubselects() {
    if (subselectLoadableEntityKeyMap == null || nRowsRead <= 1) {
        LOG.tracef("Skipping create subselects because there are fewer than 2 results, so query by key is more efficient.", getClass().getName());
        // early return
        return;
    }
    final Map<String, int[]> namedParameterLocMap = ResultSetProcessorHelper.buildNamedParameterLocMap(queryParameters, namedParameterContext);
    final String subselectQueryString = SubselectFetch.createSubselectFetchQueryFragment(queryParameters);
    for (Map.Entry<EntityReference, Set<EntityKey>> entry : subselectLoadableEntityKeyMap.entrySet()) {
        if (!entry.getKey().getEntityPersister().hasSubselectLoadableCollections()) {
            continue;
        }
        SubselectFetch subselectFetch = new SubselectFetch(subselectQueryString, aliasResolutionContext.resolveSqlTableAliasFromQuerySpaceUid(entry.getKey().getQuerySpaceUid()), (Loadable) entry.getKey().getEntityPersister(), queryParameters, entry.getValue(), namedParameterLocMap);
        for (EntityKey key : entry.getValue()) {
            session.getPersistenceContext().getBatchFetchQueue().addSubselect(key, subselectFetch);
        }
    }
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) EntityReference(org.hibernate.loader.plan.spi.EntityReference) HashMap(java.util.HashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 19 with EntityKey

use of org.hibernate.engine.spi.EntityKey in project hibernate-orm by hibernate.

the class ResultSetProcessingContextImpl method registerNonExists.

private void registerNonExists(EntityFetch fetch) {
    final EntityType fetchedType = fetch.getFetchedType();
    if (!fetchedType.isOneToOne()) {
        return;
    }
    final EntityReferenceProcessingState fetchOwnerState = getOwnerProcessingState(fetch);
    if (fetchOwnerState == null) {
        throw new IllegalStateException("Could not locate fetch owner state");
    }
    final EntityKey ownerEntityKey = fetchOwnerState.getEntityKey();
    if (ownerEntityKey == null) {
        throw new IllegalStateException("Could not locate fetch owner EntityKey");
    }
    session.getPersistenceContext().addNullProperty(ownerEntityKey, fetchedType.getPropertyName());
}
Also used : EntityType(org.hibernate.type.EntityType) EntityKey(org.hibernate.engine.spi.EntityKey)

Example 20 with EntityKey

use of org.hibernate.engine.spi.EntityKey in project hibernate-orm by hibernate.

the class ResultSetProcessingContextImpl method getProcessingState.

@Override
public EntityReferenceProcessingState getProcessingState(final EntityReference entityReference) {
    if (identifierResolutionContextMap == null) {
        identifierResolutionContextMap = new IdentityHashMap<>();
    }
    EntityReferenceProcessingState context = identifierResolutionContextMap.get(entityReference);
    if (context == null) {
        context = new EntityReferenceProcessingState() {

            private boolean wasMissingIdentifier;

            private Object identifierHydratedForm;

            private EntityKey entityKey;

            private Object[] hydratedState;

            private Object entityInstance;

            @Override
            public EntityReference getEntityReference() {
                return entityReference;
            }

            @Override
            public void registerMissingIdentifier() {
                if (!EntityFetch.class.isInstance(entityReference)) {
                    throw new IllegalStateException("Missing return row identifier");
                }
                ResultSetProcessingContextImpl.this.registerNonExists((EntityFetch) entityReference);
                wasMissingIdentifier = true;
            }

            @Override
            public boolean isMissingIdentifier() {
                return wasMissingIdentifier;
            }

            @Override
            public void registerIdentifierHydratedForm(Object identifierHydratedForm) {
                this.identifierHydratedForm = identifierHydratedForm;
            }

            @Override
            public Object getIdentifierHydratedForm() {
                return identifierHydratedForm;
            }

            @Override
            public void registerEntityKey(EntityKey entityKey) {
                this.entityKey = entityKey;
            }

            @Override
            public EntityKey getEntityKey() {
                return entityKey;
            }

            @Override
            public void registerHydratedState(Object[] hydratedState) {
                this.hydratedState = hydratedState;
            }

            @Override
            public Object[] getHydratedState() {
                return hydratedState;
            }

            @Override
            public void registerEntityInstance(Object entityInstance) {
                this.entityInstance = entityInstance;
            }

            @Override
            public Object getEntityInstance() {
                return entityInstance;
            }
        };
        identifierResolutionContextMap.put(entityReference, context);
    }
    return context;
}
Also used : EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) EntityKey(org.hibernate.engine.spi.EntityKey) EntityReference(org.hibernate.loader.plan.spi.EntityReference)

Aggregations

EntityKey (org.hibernate.engine.spi.EntityKey)43 Serializable (java.io.Serializable)13 EntityEntry (org.hibernate.engine.spi.EntityEntry)12 EntityPersister (org.hibernate.persister.entity.EntityPersister)10 EventSource (org.hibernate.event.spi.EventSource)8 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)7 List (java.util.List)6 EntityType (org.hibernate.type.EntityType)6 ArrayList (java.util.ArrayList)5 SQLException (java.sql.SQLException)4 EntityReferenceProcessingState (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessingContext.EntityReferenceProcessingState)4 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 HibernateException (org.hibernate.HibernateException)3 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)3 SubselectFetch (org.hibernate.engine.spi.SubselectFetch)3 HibernateProxy (org.hibernate.proxy.HibernateProxy)3 Type (org.hibernate.type.Type)3 HashSet (java.util.HashSet)2