use of org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor in project hibernate-orm by hibernate.
the class BytecodeEnhancementMetadataPojoImpl method injectInterceptor.
@Override
public LazyAttributeLoadingInterceptor injectInterceptor(Object entity, SharedSessionContractImplementor session) {
if (!enhancedForLazyLoading) {
throw new NotInstrumentedException("Entity class [" + entityClass.getName() + "] is not enhanced for lazy loading");
}
if (!entityClass.isInstance(entity)) {
throw new IllegalArgumentException(String.format("Passed entity instance [%s] is not of expected type [%s]", entity, getEntityName()));
}
final LazyAttributeLoadingInterceptor interceptor = new LazyAttributeLoadingInterceptor(getEntityName(), lazyAttributesMetadata.getLazyAttributeNames(), session);
((PersistentAttributeInterceptable) entity).$$_hibernate_setInterceptor(interceptor);
return interceptor;
}
use of org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor in project hibernate-orm by hibernate.
the class PojoEntityInstantiator method applyInterception.
@Override
protected Object applyInterception(Object entity) {
if (!applyBytecodeInterception) {
return entity;
}
PersistentAttributeInterceptor interceptor = new LazyAttributeLoadingInterceptor(entityMetamodel.getName(), entityMetamodel.getBytecodeEnhancementMetadata().getLazyAttributesMetadata().getLazyAttributeNames(), null);
((PersistentAttributeInterceptable) entity).$$_hibernate_setInterceptor(interceptor);
return entity;
}
use of org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor in project hibernate-orm by hibernate.
the class PersistenceUtilHelper method isLoadedWithoutReference.
/**
* Is the given attribute (by name) loaded? This form must take care to not access the attribute (trigger
* initialization).
*
* @param entity The entity
* @param attributeName The name of the attribute to check
* @param cache The cache we maintain of attribute resolutions
*
* @return The LoadState
*/
public static LoadState isLoadedWithoutReference(Object entity, String attributeName, MetadataCache cache) {
boolean sureFromUs = false;
if (entity instanceof HibernateProxy) {
LazyInitializer li = ((HibernateProxy) entity).getHibernateLazyInitializer();
if (li.isUninitialized()) {
// we have an uninitialized proxy, the attribute cannot be loaded
return LoadState.NOT_LOADED;
} else {
// swap the proxy with target (for proper class name resolution)
entity = li.getImplementation();
}
sureFromUs = true;
}
// we are instrumenting but we can't assume we are the only ones
if (entity instanceof PersistentAttributeInterceptable) {
final LazyAttributeLoadingInterceptor interceptor = extractInterceptor((PersistentAttributeInterceptable) entity);
final boolean isInitialized = interceptor == null || interceptor.isAttributeLoaded(attributeName);
LoadState state;
if (isInitialized && interceptor != null) {
// it's ours, we can read
try {
final Class entityClass = entity.getClass();
final Object attributeValue = cache.getClassMetadata(entityClass).getAttributeAccess(attributeName).extractValue(entity);
state = isLoaded(attributeValue);
// it's ours so we know it's loaded
if (state == LoadState.UNKNOWN) {
state = LoadState.LOADED;
}
} catch (AttributeExtractionException ignore) {
state = LoadState.UNKNOWN;
}
} else if (interceptor != null) {
state = LoadState.NOT_LOADED;
} else if (sureFromUs) {
// it's ours, we can read
try {
final Class entityClass = entity.getClass();
final Object attributeValue = cache.getClassMetadata(entityClass).getAttributeAccess(attributeName).extractValue(entity);
state = isLoaded(attributeValue);
// it's ours so we know it's loaded
if (state == LoadState.UNKNOWN) {
state = LoadState.LOADED;
}
} catch (AttributeExtractionException ignore) {
state = LoadState.UNKNOWN;
}
} else {
state = LoadState.UNKNOWN;
}
return state;
} else {
return LoadState.UNKNOWN;
}
}
use of org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor in project hibernate-orm by hibernate.
the class Cascade method cascade.
/**
* Cascade an action from the parent entity instance to all its children. This
* form is typically called from within cascade actions.
*
* @param persister The parent's entity persister
* @param parent The parent reference.
* @param anything Anything ;) Typically some form of cascade-local cache
* which is specific to each CascadingAction type
*/
public static void cascade(final CascadingAction action, final CascadePoint cascadePoint, final EventSource eventSource, final EntityPersister persister, final Object parent, final Object anything) throws HibernateException {
if (persister.hasCascades() || action.requiresNoCascadeChecking()) {
// performance opt
final boolean traceEnabled = LOG.isTraceEnabled();
if (traceEnabled) {
LOG.tracev("Processing cascade {0} for: {1}", action, persister.getEntityName());
}
final Type[] types = persister.getPropertyTypes();
final String[] propertyNames = persister.getPropertyNames();
final CascadeStyle[] cascadeStyles = persister.getPropertyCascadeStyles();
final boolean hasUninitializedLazyProperties = persister.hasUninitializedLazyProperties(parent);
final int componentPathStackDepth = 0;
for (int i = 0; i < types.length; i++) {
final CascadeStyle style = cascadeStyles[i];
final String propertyName = propertyNames[i];
final boolean isUninitializedProperty = hasUninitializedLazyProperties && !persister.getInstrumentationMetadata().isAttributeLoaded(parent, propertyName);
if (style.doCascade(action)) {
final Object child;
if (isUninitializedProperty) {
// cascading to an uninitialized, lazy value.
if (types[i].isCollectionType()) {
// The collection does not need to be loaded from the DB.
// CollectionType#resolve will return an uninitialized PersistentCollection.
// The action will initialize the collection later, if necessary.
child = types[i].resolve(LazyPropertyInitializer.UNFETCHED_PROPERTY, eventSource, parent);
// TODO: it would be nice to be able to set the attribute in parent using
// persister.setPropertyValue( parent, i, child ).
// Unfortunately, that would cause the uninitialized collection to be
// loaded from the DB.
} else if (action.performOnLazyProperty()) {
// The (non-collection) attribute needs to be initialized so that
// the action can be performed on the initialized attribute.
LazyAttributeLoadingInterceptor interceptor = persister.getInstrumentationMetadata().extractInterceptor(parent);
child = interceptor.fetchAttribute(parent, propertyName);
} else {
// Nothing to do, so just skip cascading to this lazy (non-collection) attribute.
continue;
}
} else {
child = persister.getPropertyValue(parent, i);
}
cascadeProperty(action, cascadePoint, eventSource, componentPathStackDepth, parent, child, types[i], style, propertyName, anything, false);
} else {
if (action.requiresNoCascadeChecking()) {
action.noCascade(eventSource, parent, persister, types[i], i);
}
// If the property is uninitialized, then there cannot be any orphans.
if (action.deleteOrphans() && !isUninitializedProperty) {
cascadeLogicalOneToOneOrphanRemoval(action, eventSource, componentPathStackDepth, parent, persister.getPropertyValue(parent, i), types[i], style, propertyName, false);
}
}
}
if (traceEnabled) {
LOG.tracev("Done processing cascade {0} for: {1}", action, persister.getEntityName());
}
}
}
use of org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method clear.
@Override
public void clear() {
for (Object o : proxiesByKey.values()) {
if (o == null) {
// entry may be GCd
continue;
}
((HibernateProxy) o).getHibernateLazyInitializer().unsetSession();
}
for (Entry<Object, EntityEntry> objectEntityEntryEntry : entityEntryContext.reentrantSafeEntityEntries()) {
// todo : I dont think this need be reentrant safe
if (objectEntityEntryEntry.getKey() instanceof PersistentAttributeInterceptable) {
final PersistentAttributeInterceptor interceptor = ((PersistentAttributeInterceptable) objectEntityEntryEntry.getKey()).$$_hibernate_getInterceptor();
if (interceptor instanceof LazyAttributeLoadingInterceptor) {
((LazyAttributeLoadingInterceptor) interceptor).unsetSession();
}
}
}
for (Map.Entry<PersistentCollection, CollectionEntry> aCollectionEntryArray : IdentityMap.concurrentEntries(collectionEntries)) {
aCollectionEntryArray.getKey().unsetSession(getSession());
}
arrayHolders.clear();
entitiesByKey.clear();
entitiesByUniqueKey.clear();
entityEntryContext.clear();
// entityEntries.clear();
parentsByChild.clear();
entitySnapshotsByKey.clear();
collectionsByKey.clear();
collectionEntries.clear();
if (unownedCollections != null) {
unownedCollections.clear();
}
proxiesByKey.clear();
nullifiableEntityKeys.clear();
if (batchFetchQueue != null) {
batchFetchQueue.clear();
}
// defaultReadOnly is unaffected by clear()
hasNonReadOnlyEntities = false;
if (loadContexts != null) {
loadContexts.cleanup();
}
naturalIdXrefDelegate.clear();
}
Aggregations