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());
}
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;
}
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();
}
}
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;
}
}
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;
}
Aggregations