use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class ReadOnlyProxyTest method testModifiableViaLazyInitializerAfterInit.
@Test
public void testModifiableViaLazyInitializerAfterInit() {
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(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 ReadOnlyProxyTest method testReadOnlyViaLazyInitializerDoesNotInit.
@Test
public void testReadOnlyViaLazyInitializerDoesNotInit() {
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);
assertFalse(Hibernate.isInitialized(dp));
dpLI.setReadOnly(true);
checkReadOnly(s, dp, true);
assertFalse(Hibernate.isInitialized(dp));
dpLI.setReadOnly(false);
checkReadOnly(s, dp, false);
assertFalse(Hibernate.isInitialized(dp));
s.flush();
checkReadOnly(s, dp, false);
assertFalse(Hibernate.isInitialized(dp));
s.getTransaction().commit();
checkReadOnly(s, dp, false);
assertFalse(Hibernate.isInitialized(dp));
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();
}
use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class ReadOnlyProxyTest method testReadOnlyViaLazyInitializerAfterInit.
@Test
public void testReadOnlyViaLazyInitializerAfterInit() {
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(dpLI.isUninitialized());
checkReadOnly(s, dp, false);
dp.setDescription("changed");
assertFalse(dpLI.isUninitialized());
assertEquals("changed", dp.getDescription());
checkReadOnly(s, dp, false);
dpLI.setReadOnly(true);
checkReadOnly(s, dp, true);
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(dpOrig.getDescription(), 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 StatefulPersistenceContext method reassociateIfUninitializedProxy.
@Override
public boolean reassociateIfUninitializedProxy(Object value) throws MappingException {
if (!Hibernate.isInitialized(value)) {
final HibernateProxy proxy = (HibernateProxy) value;
final LazyInitializer li = proxy.getHibernateLazyInitializer();
reassociateProxy(li, proxy);
return true;
} else {
return false;
}
}
use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class SessionImpl method bestGuessEntityName.
@Override
public String bestGuessEntityName(Object object) {
if (object instanceof HibernateProxy) {
LazyInitializer initializer = ((HibernateProxy) object).getHibernateLazyInitializer();
// so make certain that we do not accidentally initialize an uninitialized proxy
if (initializer.isUninitialized()) {
return initializer.getEntityName();
}
object = initializer.getImplementation();
}
EntityEntry entry = persistenceContext.getEntry(object);
if (entry == null) {
return guessEntityName(object);
} else {
return entry.getPersister().getEntityName();
}
}
Aggregations