use of org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor in project hibernate-orm by hibernate.
the class StatelessSessionImpl method fetch.
@Override
public void fetch(Object association) {
checkOpen();
PersistenceContext persistenceContext = getPersistenceContext();
if (association instanceof HibernateProxy) {
LazyInitializer initializer = ((HibernateProxy) association).getHibernateLazyInitializer();
if (initializer.isUninitialized()) {
String entityName = initializer.getEntityName();
Object id = initializer.getIdentifier();
initializer.setSession(this);
persistenceContext.beforeLoad();
try {
// forces the load to occur
Object entity = initializer.getImplementation();
if (entity == null) {
getFactory().getEntityNotFoundDelegate().handleEntityNotFound(entityName, id);
}
initializer.setImplementation(entity);
} finally {
initializer.unsetSession();
persistenceContext.afterLoad();
if (persistenceContext.isLoadFinished()) {
persistenceContext.clear();
}
}
}
} else if (association instanceof PersistentAttributeInterceptable) {
final PersistentAttributeInterceptor interceptor = ((PersistentAttributeInterceptable) association).$$_hibernate_getInterceptor();
if (interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
EnhancementAsProxyLazinessInterceptor proxyInterceptor = (EnhancementAsProxyLazinessInterceptor) interceptor;
proxyInterceptor.setSession(this);
try {
proxyInterceptor.forceInitialize(association, null);
} finally {
proxyInterceptor.unsetSession();
if (persistenceContext.isLoadFinished()) {
persistenceContext.clear();
}
}
}
} else if (association instanceof PersistentCollection) {
PersistentCollection<?> persistentCollection = (PersistentCollection<?>) association;
if (!persistentCollection.wasInitialized()) {
final CollectionPersister collectionDescriptor = getFactory().getRuntimeMetamodels().getMappingMetamodel().getCollectionDescriptor(persistentCollection.getRole());
Object key = persistentCollection.getKey();
persistenceContext.addUninitializedCollection(collectionDescriptor, persistentCollection, key);
persistentCollection.setCurrentSession(this);
try {
collectionDescriptor.initialize(key, this);
} finally {
persistentCollection.unsetSession(this);
if (persistenceContext.isLoadFinished()) {
persistenceContext.clear();
}
}
}
}
}
use of org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor in project hibernate-reactive by hibernate.
the class DefaultReactiveMergeEventListener method unproxyManagedForDetachedMerging.
private Object unproxyManagedForDetachedMerging(Object incoming, Object managed, EntityPersister persister, EventSource source) {
if (managed instanceof HibernateProxy) {
return source.getPersistenceContextInternal().unproxy(managed);
}
if (incoming instanceof PersistentAttributeInterceptable && persister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()) {
final PersistentAttributeInterceptor incomingInterceptor = ((PersistentAttributeInterceptable) incoming).$$_hibernate_getInterceptor();
final PersistentAttributeInterceptor managedInterceptor = ((PersistentAttributeInterceptable) managed).$$_hibernate_getInterceptor();
// if the managed entity is not a proxy, we can just return it
if (!(managedInterceptor instanceof EnhancementAsProxyLazinessInterceptor)) {
return managed;
}
// if the incoming entity is still a proxy there is no need to force initialization of the managed one
if (incomingInterceptor instanceof EnhancementAsProxyLazinessInterceptor) {
return managed;
}
// TODO: probably needs to be made async!
return persister.initializeEnhancedEntityUsedAsProxy(managed, null, source);
}
return managed;
}
use of org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor in project hibernate-reactive by hibernate.
the class DefaultReactiveMergeEventListener method reactiveOnMerge.
/**
* Handle the given merge event.
*
* @param event The merge event to be handled.
*/
@Override
public CompletionStage<Void> reactiveOnMerge(MergeEvent event, MergeContext copyCache) throws HibernateException {
final EventSource source = event.getSession();
final Object original = event.getOriginal();
if (original != null) {
final Object entity;
if (original instanceof HibernateProxy) {
LazyInitializer li = ((HibernateProxy) original).getHibernateLazyInitializer();
if (li.isUninitialized()) {
LOG.trace("Ignoring uninitialized proxy");
event.setResult(source.load(li.getEntityName(), li.getIdentifier()));
// EARLY EXIT!
return voidFuture();
} else {
entity = li.getImplementation();
}
} else if (original instanceof PersistentAttributeInterceptable) {
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) original;
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
if (interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
final EnhancementAsProxyLazinessInterceptor proxyInterceptor = (EnhancementAsProxyLazinessInterceptor) interceptor;
LOG.trace("Ignoring uninitialized enhanced-proxy");
// no need to go async, AFAICT ?
event.setResult(source.load(proxyInterceptor.getEntityName(), (Serializable) proxyInterceptor.getIdentifier()));
// EARLY EXIT!
return voidFuture();
} else {
entity = original;
}
} else {
entity = original;
}
if (copyCache.containsKey(entity) && (copyCache.isOperatedOn(entity))) {
LOG.trace("Already in merge process");
event.setResult(entity);
} else {
if (copyCache.containsKey(entity)) {
LOG.trace("Already in copyCache; setting in merge process");
copyCache.setOperatedOn(entity, true);
}
event.setEntity(entity);
EntityState entityState = null;
// Check the persistence context for an entry relating to this
// entity to be merged...
final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
EntityEntry entry = persistenceContext.getEntry(entity);
if (entry == null) {
EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
Serializable id = persister.getIdentifier(entity, source);
if (id != null) {
final EntityKey key = source.generateEntityKey(id, persister);
final Object managedEntity = persistenceContext.getEntity(key);
entry = persistenceContext.getEntry(managedEntity);
if (entry != null) {
// we have specialized case of a detached entity from the
// perspective of the merge operation. Specifically, we
// have an incoming entity instance which has a corresponding
// entry in the current persistence context, but registered
// under a different entity instance
entityState = DETACHED;
}
}
}
if (entityState == null) {
entityState = EntityState.getEntityState(entity, event.getEntityName(), entry, source, false);
}
switch(entityState) {
case DETACHED:
return entityIsDetached(event, copyCache);
case TRANSIENT:
return entityIsTransient(event, copyCache);
case PERSISTENT:
return entityIsPersistent(event, copyCache);
default:
// DELETED
throw new ObjectDeletedException("deleted instance passed to merge", null, EventUtil.getLoggableName(event.getEntityName(), entity));
}
}
}
return voidFuture();
}
Aggregations