use of jakarta.persistence.spi.LoadState in project hibernate-orm by hibernate.
the class PersistenceUnitUtilImpl method isLoaded.
@Override
public boolean isLoaded(Object entity, String attributeName) {
// added log message to help with HHH-7454, if state == LoadState,NOT_LOADED, returning true or false is not accurate.
log.debug("PersistenceUnitUtil#isLoaded is not always accurate; consider using EntityManager#contains instead");
LoadState state = PersistenceUtilHelper.isLoadedWithoutReference(entity, attributeName, cache);
if (state == LoadState.LOADED) {
return true;
} else if (state == LoadState.NOT_LOADED) {
return false;
} else {
return PersistenceUtilHelper.isLoadedWithReference(entity, attributeName, cache) != LoadState.NOT_LOADED;
}
}
use of jakarta.persistence.spi.LoadState 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 BytecodeLazyAttributeInterceptor 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;
}
}
Aggregations