use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class ReadOnlyProxyTest method testModifiableViaLazyInitializerBeforeInit.
@Test
public void testModifiableViaLazyInitializerBeforeInit() {
DataPoint dpOrig = createDataPoint(CacheMode.IGNORE);
Session s = openSession();
s.setCacheMode(CacheMode.IGNORE);
s.beginTransaction();
DataPoint dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
assertTrue(dp instanceof HibernateProxy);
LazyInitializer dpLI = ((HibernateProxy) dp).getHibernateLazyInitializer();
assertTrue(dp instanceof HibernateProxy);
assertTrue(dpLI.isUninitialized());
checkReadOnly(s, dp, false);
dp.setDescription("changed");
assertFalse(dpLI.isUninitialized());
assertEquals("changed", dp.getDescription());
checkReadOnly(s, dp, false);
s.flush();
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
dp = (DataPoint) s.get(DataPoint.class, dpOrig.getId());
assertEquals(dpOrig.getId(), dp.getId());
assertEquals("changed", dp.getDescription());
assertEquals(dpOrig.getX(), dp.getX());
assertEquals(dpOrig.getY(), dp.getY());
s.delete(dp);
s.getTransaction().commit();
s.close();
}
use of org.hibernate.proxy.LazyInitializer 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.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class Hibernate method unproxy.
/**
* Unproxies a {@link HibernateProxy}. If the proxy is uninitialized, it automatically triggers an initialization.
* In case the supplied object is null or not a proxy, the object will be returned as-is.
*
* @param proxy the {@link HibernateProxy} to be unproxied
* @return the proxy's underlying implementation object, or the supplied object otherwise
*/
public static Object unproxy(Object proxy) {
if (proxy instanceof HibernateProxy) {
HibernateProxy hibernateProxy = (HibernateProxy) proxy;
LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer();
return initializer.getImplementation();
} else {
return proxy;
}
}
use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method unproxy.
@Override
public Object unproxy(Object maybeProxy) throws HibernateException {
if (maybeProxy instanceof HibernateProxy) {
final HibernateProxy proxy = (HibernateProxy) maybeProxy;
final LazyInitializer li = proxy.getHibernateLazyInitializer();
if (li.isUninitialized()) {
throw new PersistentObjectException("object was an uninitialized proxy for " + li.getEntityName());
}
// unwrap the object and return
return li.getImplementation();
} else {
return maybeProxy;
}
}
use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method unproxyAndReassociate.
@Override
public Object unproxyAndReassociate(Object maybeProxy) throws HibernateException {
if (maybeProxy instanceof HibernateProxy) {
final HibernateProxy proxy = (HibernateProxy) maybeProxy;
final LazyInitializer li = proxy.getHibernateLazyInitializer();
reassociateProxy(li, proxy);
// initialize + unwrap the object and return it
return li.getImplementation();
} else {
return maybeProxy;
}
}
Aggregations