use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class EntityInstantiator method replaceNonAuditIdProxies.
@SuppressWarnings({ "unchecked" })
private void replaceNonAuditIdProxies(Map versionsEntity, Number revision) {
final Map originalId = (Map) versionsEntity.get(enversService.getAuditEntitiesConfiguration().getOriginalIdPropName());
for (Object key : originalId.keySet()) {
final Object value = originalId.get(key);
if (value instanceof HibernateProxy) {
final HibernateProxy hibernateProxy = (HibernateProxy) value;
final LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer();
final String entityName = initializer.getEntityName();
final Serializable entityId = initializer.getIdentifier();
if (enversService.getEntitiesConfigurations().isVersioned(entityName)) {
final String entityClassName = enversService.getEntitiesConfigurations().get(entityName).getEntityClassName();
final Class entityClass = ReflectionTools.loadClass(entityClassName, enversService.getClassLoaderService());
final ToOneDelegateSessionImplementor delegate = new ToOneDelegateSessionImplementor(versionsReader, entityClass, entityId, revision, RevisionType.DEL.equals(versionsEntity.get(enversService.getAuditEntitiesConfiguration().getRevisionTypePropName())), enversService);
originalId.put(key, versionsReader.getSessionImplementor().getFactory().getMetamodel().entityPersister(entityName).createProxy(entityId, delegate));
}
}
}
}
use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class ProxyIdentifier method testProxyIdentifier.
@Test
public void testProxyIdentifier() {
TargetNotAuditedEntity rev1 = getAuditReader().find(TargetNotAuditedEntity.class, tnae1.getId(), 1);
Assert.assertTrue(rev1.getReference() instanceof HibernateProxy);
HibernateProxy proxyCreateByEnvers = (HibernateProxy) rev1.getReference();
LazyInitializer lazyInitializer = proxyCreateByEnvers.getHibernateLazyInitializer();
Assert.assertTrue(lazyInitializer.isUninitialized());
Assert.assertNotNull(lazyInitializer.getIdentifier());
Assert.assertEquals(tnae1.getId(), lazyInitializer.getIdentifier());
Assert.assertTrue(lazyInitializer.isUninitialized());
Assert.assertEquals(uste1.getId(), rev1.getReference().getId());
Assert.assertEquals(uste1.getStr(), rev1.getReference().getStr());
Assert.assertFalse(lazyInitializer.isUninitialized());
}
use of org.hibernate.proxy.LazyInitializer in project head by mifos.
the class InterceptHelper method getClazz.
private Class getClazz(Object obj) {
try {
HibernateProxy hibernateProxy = (HibernateProxy) obj;
LazyInitializer lazyInitializer = hibernateProxy.getHibernateLazyInitializer();
return lazyInitializer.getPersistentClass();
} catch (ClassCastException e) {
return obj.getClass();
}
}
use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class ReadOnlyProxyTest method checkReadOnly.
private void checkReadOnly(Session s, Object proxy, boolean expectedReadOnly) {
assertTrue(proxy instanceof HibernateProxy);
LazyInitializer li = ((HibernateProxy) proxy).getHibernateLazyInitializer();
assertSame(s, li.getSession());
assertEquals(expectedReadOnly, s.isReadOnly(proxy));
assertEquals(expectedReadOnly, li.isReadOnly());
assertEquals(Hibernate.isInitialized(proxy), !li.isUninitialized());
if (Hibernate.isInitialized(proxy)) {
assertEquals(expectedReadOnly, s.isReadOnly(li.getImplementation()));
}
}
use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class ReadOnlyProxyTest method testReadOnlyViaLazyInitializerNoChangeAfterInit.
@Test
public void testReadOnlyViaLazyInitializerNoChangeAfterInit() {
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();
checkReadOnly(s, dp, false);
assertTrue(dpLI.isUninitialized());
Hibernate.initialize(dp);
assertFalse(dpLI.isUninitialized());
checkReadOnly(s, dp, false);
s.getTransaction().commit();
s.close();
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
s.beginTransaction();
dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
assertTrue(dp instanceof HibernateProxy);
dpLI = ((HibernateProxy) dp).getHibernateLazyInitializer();
dpLI.setReadOnly(true);
checkReadOnly(s, dp, true);
assertTrue(dpLI.isUninitialized());
Hibernate.initialize(dp);
assertFalse(dpLI.isUninitialized());
checkReadOnly(s, dp, true);
s.getTransaction().commit();
s.close();
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
s.beginTransaction();
dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
assertTrue(dp instanceof HibernateProxy);
dpLI = ((HibernateProxy) dp).getHibernateLazyInitializer();
dpLI.setReadOnly(true);
checkReadOnly(s, dp, true);
assertTrue(dpLI.isUninitialized());
dpLI.setReadOnly(false);
checkReadOnly(s, dp, false);
assertTrue(dpLI.isUninitialized());
Hibernate.initialize(dp);
assertFalse(dpLI.isUninitialized());
checkReadOnly(s, dp, false);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
dp = (DataPoint) s.get(DataPoint.class, dpOrig.getId());
assertEquals(dpOrig.getId(), dp.getId());
assertEquals(dpOrig.getDescription(), dp.getDescription());
assertEquals(dpOrig.getX(), dp.getX());
assertEquals(dpOrig.getY(), dp.getY());
s.delete(dp);
s.getTransaction().commit();
s.close();
}
Aggregations