Search in sources :

Example 21 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 22 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)

Example 23 with EntityKey

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

the class BasicLazyInitializer method getReplacement.

private Object getReplacement() {
    final SharedSessionContractImplementor session = getSession();
    if (isUninitialized() && session != null && session.isOpen()) {
        final EntityKey key = session.generateEntityKey(getIdentifier(), session.getFactory().getMetamodel().entityPersister(getEntityName()));
        final Object entity = session.getPersistenceContext().getEntity(key);
        if (entity != null) {
            setImplementation(entity);
        }
    }
    if (isUninitialized()) {
        if (replacement == null) {
            replacement = serializableProxy();
        }
        return replacement;
    } else {
        return getTarget();
    }
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) MarkerObject(org.hibernate.internal.util.MarkerObject)

Example 24 with EntityKey

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

the class OneToOneType method isNull.

@Override
public boolean isNull(Object owner, SharedSessionContractImplementor session) {
    if (propertyName != null) {
        final EntityPersister ownerPersister = session.getFactory().getMetamodel().entityPersister(entityName);
        final Serializable id = session.getContextEntityIdentifier(owner);
        final EntityKey entityKey = session.generateEntityKey(id, ownerPersister);
        return session.getPersistenceContext().isPropertyNull(entityKey, getPropertyName());
    } else {
        return false;
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityKey(org.hibernate.engine.spi.EntityKey) Serializable(java.io.Serializable)

Example 25 with EntityKey

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

the class AbstractEntityTuplizer method determineEntityIdPersistIfNecessary.

private static Serializable determineEntityIdPersistIfNecessary(Object entity, AssociationType associationType, SharedSessionContractImplementor session, SessionFactoryImplementor sessionFactory) {
    if (entity == null) {
        return null;
    }
    if (HibernateProxy.class.isInstance(entity)) {
        // entity is a proxy, so we know it is not transient; just return ID from proxy
        return ((HibernateProxy) entity).getHibernateLazyInitializer().getIdentifier();
    }
    if (session != null) {
        final EntityEntry pcEntry = session.getPersistenceContext().getEntry(entity);
        if (pcEntry != null) {
            // entity managed; return ID.
            return pcEntry.getId();
        }
    }
    final EntityPersister persister = resolveEntityPersister(entity, associationType, session, sessionFactory);
    Serializable entityId = persister.getIdentifier(entity, session);
    if (entityId == null) {
        if (session != null) {
            // if we have a session, then follow the HHH-11328 requirements
            entityId = persistTransientEntity(entity, session);
        }
    // otherwise just let it be null HHH-11274
    } else {
        if (session != null) {
            // if the entity is in the process of being merged, it may be stored in the
            // PC already, but doesn't have an EntityEntry yet. If this is the case,
            // then don't persist even if it is transient because doing so can lead
            // to having 2 entities in the PC with the same ID (HHH-11328).
            final EntityKey entityKey = session.generateEntityKey(entityId, persister);
            if (session.getPersistenceContext().getEntity(entityKey) == null && ForeignKeys.isTransient(persister.getEntityName(), entity, null, session)) {
                // entity is transient and it is not in the PersistenceContext.
                // entity needs to be persisted.
                persistTransientEntity(entity, session);
            }
        }
    }
    return entityId;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityKey(org.hibernate.engine.spi.EntityKey) EntityEntry(org.hibernate.engine.spi.EntityEntry) Serializable(java.io.Serializable)

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